1

I have a popup class for showing some message across my entire app. I have to init an instance and add it as a child of a currently appearing controller then show the popup view whenever I want:

func popMessage() {
    let popup = MyPopup()
    self.addChildViewController(popup)
    self.view.addSubView(popup.view)
    popup.didMoveToParentViewController(self)

    popup.show()
}

Sometimes the current view controller is embedded in UINavigationController or UITabBarController. In case of making my popup view show in whole screen size without being clipped by the navigation bar or tab bar, I have to add it as child of the navigation controller or tab bar controller. This is annoying when I decide to change controller hierarchy.

How can I make it easier like using an UIAlertViewController:

let popup = MyPopup()
popup.show() 
or 
self.presentViewController(popup)

Pop it anywhere and make it the only focus on screen without regarding which parent view controller the popup should have?

bluenowhere
  • 2,683
  • 5
  • 24
  • 37

1 Answers1

3

You can present your popup on root view controller of application window.

let popUp = MyPopup()
let appDelegate = UIApplication.sharedApplication().delegate  as! AppDelegate
let appWindow = appDelegate.window
var topViewController = window?.rootViewController

    while topViewController?.presentedViewController != nil
    {
        topViewController = topViewController?.presentedViewController
    }

topViewController.presentViewController(popUp, animated: true, completion: nil)
Ravi
  • 2,441
  • 1
  • 11
  • 30
  • That helps! And what if I want to pop it with animation using `UIView.animateWithDuration`rather than presenting it modally? Is adding popup as a child view controller the only way to animate it's own view? – bluenowhere Mar 22 '16 at 07:44
  • 1
    if you are looking for present view with custom animation... http://stackoverflow.com/questions/19931710/how-to-custom-modal-view-controller-presenting-animation – Ravi Mar 22 '16 at 08:48