Attempt to present vc whose view is not in the window hierarchy

10,557

Solution 1

Add extention given bellow to your application and use it any where you want to present any view controller, it works for me hope it helps you.

//MARK: - UIApplication Extension
extension UIApplication {
    class func topViewController(viewController: UIViewController? = UIApplication.shared.keyWindow?.rootViewController) -> UIViewController? {
        if let nav = viewController as? UINavigationController {
            return topViewController(viewController: nav.visibleViewController)
        }
        if let tab = viewController as? UITabBarController {
            if let selected = tab.selectedViewController {
                return topViewController(viewController: selected)
            }
        }
        if let presented = viewController?.presentedViewController {
            return topViewController(viewController: presented)
        }
        return viewController
    }
}

And Present it by following code:

 UIApplication.topViewController()?.present(vc, animated: true, completion: nil)

Solution 2

If you are attempting to present a modal view controller within the viewDidLoad method, can try to move this call to the viewDidAppear: method.

Solution 3

You need to find top view controller

From

https://stackoverflow.com/a/26859650/4601900

extension UIViewController {
func topMostViewController() -> UIViewController {
    // Handling Modal views
    if let presentedViewController = self.presentedViewController {
        return presentedViewController.topMostViewController()
    }
    // Handling UIViewController's added as subviews to some other views.
    else {
        for view in self.view.subviews
        {
            // Key property which most of us are unaware of / rarely use.
            if let subViewController = view.nextResponder() {
                if subViewController is UIViewController {
                    let viewController = subViewController as UIViewController
                    return viewController.topMostViewController()
                }
            }
        }
        return self
    }
}
}

extension UITabBarController {
override func topMostViewController() -> UIViewController {
    return self.selectedViewController!.topMostViewController()
}
}

 extension UINavigationController {
override func topMostViewController() -> UIViewController {
    return self.visibleViewController.topMostViewController()
}

}

How to use

  UIApplication.sharedApplication().keyWindow!.rootViewController!.topMostViewController()
Share:
10,557

Related videos on Youtube

Aya Aboud
Author by

Aya Aboud

Updated on June 04, 2022

Comments

  • Aya Aboud
    Aya Aboud almost 2 years

    I'm trying to open file in thread and here is my code:

    DispatchQueue.main.async(execute: { () -> Void in
        var documentsURL = (FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)).appendPathComponent(“File.pdf")
        self.docController = UIDocumentInteractionController.init(url: documentsURL as URL)
        self.docController?.delegate = self as? UIDocumentInteractionControllerDelegate
        self.docController?.presentPreview(animated: true)
        self.docController?.presentOpenInMenu(from: CGRect.zero, in: self.view, animated: true)
    })
    

    when move to main screen this warning is displayed and file not open

    Warning: Attempt to present <_UIDocumentActivityViewController: 0x...> on <HCM.PrintVacationDecisionVC: 0x...> whose view is not in the window hierarchy! 
    

    Any help to solve this problem?

    • Larme
      Larme almost 7 years
      PrintVacationDecisionVC is not visible, so it can't present the UIDocumentInteractionController.
    • Aya Aboud
      Aya Aboud almost 7 years
      Link not solve my problem which i make long search before post problem
  • Aya Aboud
    Aya Aboud almost 7 years
    when i try to use code there are error in line of window (type vc has no member window) and getCurrentViewController undefined
  • Prashant Tukadiya
    Prashant Tukadiya almost 7 years
    Did you try with AppDelegate's window ?
  • Aya Aboud
    Aya Aboud almost 7 years
    Did you mean use AppDelegate instead of self
  • Prashant Tukadiya
    Prashant Tukadiya almost 7 years
    try like ((UIApplication.sharedApplication.deletgate) as! AppDelegate).window
  • Aya Aboud
    Aya Aboud almost 7 years
    unresolved identifier (getCurrentViewController)
  • Prashant Tukadiya
    Prashant Tukadiya almost 7 years
    I have updated Answer , It will be more helpful to you
  • Aya Aboud
    Aya Aboud almost 7 years
  • Alex Merlin
    Alex Merlin almost 6 years
    This Works! Thank You!
  • Lance Samaria
    Lance Samaria over 5 years
    Nice solution! I've been looking around for a couple of hours and it's the only thing that I could find that works
  • Arash Afsharpour
    Arash Afsharpour about 5 years
    I still have the same issue even after trying this, I wanna present a ViewController in my MainViewController after checking if the user is authenticated, but it doesn't work, I also do Pure swift code for views, no storyboard at all