presenting ViewController with NavigationViewController swift

后端 未结 5 1727
小鲜肉
小鲜肉 2020-12-02 07:02

I have system \"NavigationViewController -> MyViewController\", and I programmatically want to present MyViewController inside a third view controller. The problem is that I

5条回答
  •  旧时难觅i
    2020-12-02 07:53

    Calling presentViewController presents the view controller modally, outside the existing navigation stack; it is not contained by your UINavigationController or any other. If you want your new view controller to have a navigation bar, you have two main options:

    Option 1. Push the new view controller onto your existing navigation stack, rather than presenting it modally:

    let VC1 = self.storyboard!.instantiateViewControllerWithIdentifier("MyViewController") as! ViewController
    self.navigationController!.pushViewController(VC1, animated: true)
    

    Option 2. Embed your new view controller into a new navigation controller and present the new navigation controller modally:

    let VC1 = self.storyboard!.instantiateViewControllerWithIdentifier("MyViewController") as! ViewController
    let navController = UINavigationController(rootViewController: VC1) // Creating a navigation controller with VC1 at the root of the navigation stack.
    self.present(navController, animated:true, completion: nil)
    

    Bear in mind that this option won't automatically include a "back" button. You'll have to build in a close mechanism yourself.

    Which one is best for you is a human interface design question, but it's normally clear what makes the most sense.

提交回复
热议问题