iosswiftuitabcontroller

How to show a ViewController (not modally) within a UITabViewController


I want to show a ViewController within a UITabController but not display it modally. In the app, there's a home screen with a bunch of buttons that segue to other ViewControllers. The other view controllers are embedded in a navigation controller. I now have a need to show one of the ViewControllers from a TodayExtension. In my app delegate I have this but it presents the RequestorViewContoller modally without the tab structure or the navigation control.

    let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
    let requestor = mainStoryboard.instantiateViewController(withIdentifier: "RequestStoryboardID") as! RequestorViewController
    let rootViewController = self.window!.rootViewController as! UITabBarController
    rootViewController.present(requestor, animated: false, completion: nil)

Solution

  • Like what you mention, if the selectedViewController of the UITabBarController is a UINavigationController, you could do a pushViewController instead.

    First, you will have to get the currently on screen UINavigationController. Then perform a pushViewController with requestor as a parameter.

    presentedNavigationController = rootViewController.selectedViewController as! UINavigationController
    presentedNavigationController.pushViewController(requestor, animated: true)