Warning: Attempt to present * on * whose view is not in the window hierarchy - swift Warning: Attempt to present * on * whose view is not in the window hierarchy - swift ios ios

Warning: Attempt to present * on * whose view is not in the window hierarchy - swift


At this point in your code the view controller's view has only been created but not added to any view hierarchy. If you want to present from that view controller as soon as possible you should do it in viewDidAppear to be safest.


In objective c:This solved my problem when presenting viewcontroller on top of mpmovieplayer

- (UIViewController*) topMostController{    UIViewController *topController = [UIApplication sharedApplication].keyWindow.rootViewController;    while (topController.presentedViewController) {        topController = topController.presentedViewController;    }    return topController;}


Swift 3

I had this keep coming up as a newbie and found that present loads modal views that can be dismissed but switching to root controller is best if you don't need to show a modal.

I was using this

let storyboard = UIStoryboard(name: "Main", bundle: nil)let vc  = storyboard?.instantiateViewController(withIdentifier: "MainAppStoryboard") as! TabbarControllerpresent(vc, animated: false, completion: nil)

Using this instead with my tabController:

let storyboard = UIStoryboard(name: "Main", bundle: nil)let view = storyboard.instantiateViewController(withIdentifier: "MainAppStoryboard") as UIViewControllerlet appDelegate = UIApplication.shared.delegate as! AppDelegate//show windowappDelegate.window?.rootViewController = view

Just adjust to a view controller if you need to switch between multiple storyboard screens.