用ViewViewController提供ViewController

我有系统“NavigationViewController – > MyViewController”,我以编程方式想在第三个视图控制器内呈现MyViewController。 问题是我没有导航栏在MyViewController中出现后。 你可以帮我吗?

var VC1 = self.storyboard.instantiateViewControllerWithIdentifier("MyViewController") as ViewController self.presentViewController(VC1, animated:true, completion: nil) 

调用presentViewController模态方式在现有的导航堆栈之外呈现视图控制器; 它不包含在你的UINavigationController或任何其他。 如果你想让你的新视图控制器有一个导航栏,你有两个主要的select:

选项1.将新视图控制器推到您现有的导航堆栈上,而不是以模态方式显示:

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

选项2.将您的新视图控制器embedded到新的导航控制器中,并以模态方式显示新的导航控制器:

 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.presentViewController(navController, animated:true, completion: nil) 

请记住,该选项不会自动包含“后退”button。 你必须自己build立一个密切的机制。

哪一个最适合你,是一个人性化的界面devise问题,但通常情况下,最明智的是什么。

我的导航栏没有显示,所以我在Swift 2 iOS 9中使用了下面的方法

  let viewController = self.storyboard?.instantiateViewControllerWithIdentifier("Dashboard") as! Dashboard // Creating a navigation controller with viewController at the root of the navigation stack. let navController = UINavigationController(rootViewController: viewController) self.presentViewController(navController, animated:true, completion: nil) 

SWIFT 3

 let VC1 = self.storyboard!.instantiateViewController(withIdentifier: "MyViewController") as! MyViewController let navController = UINavigationController(rootViewController: VC1) self.present(navController, animated:true, completion: nil) 

我使用了UIViewController的扩展和一个结构来确保我的当前视图是从collections夹中显示的

1.构build一个全球布尔

 struct PresentedFromFavourites { static var comingFromFav = false} 

2.UIVeiwController扩展名:通过“stefandouganhyde – Option 2”在第二个选项中模态地显示并解决

 extension UIViewController { func returnToFavourites() { // you return to the storyboard wanted by changing the name let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil) let mainNavigationController = storyBoard.instantiateViewController(withIdentifier: "HomeNav") as! UINavigationController // Set animated to false let favViewController = storyBoard.instantiateViewController(withIdentifier: "Favourites") self.present(mainNavigationController, animated: false, completion: { mainNavigationController.pushViewController(favViewController, animated: false) }) } // call this function in viewDidLoad() // func addBackToFavouritesButton() { if PresentedFromFavourites.comingFromFav { //Create a button // I found this good for most size classes let buttonHeight = (self.navigationController?.navigationBar.frame.size.height)! - 15 let rect = CGRect(x: 2, y: 8, width: buttonHeight, height: buttonHeight) let aButton = UIButton(frame: rect) // Down a back arrow image from icon8 for free and add it to your image assets aButton.setImage(#imageLiteral(resourceName: "backArrow"), for: .normal) aButton.backgroundColor = UIColor.clear aButton.addTarget(self, action:#selector(self.returnToFavourites), for: .touchUpInside) self.navigationController?.navigationBar.addSubview(aButton) PresentedFromFavourites.comingFromFav = false } }}