ios 使用 NavigationViewController swift 呈现 ViewController
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25444213/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
presenting ViewController with NavigationViewController swift
提问by Yury Alexandrov
I have system "NavigationViewController -> MyViewController", and I programmatically want to present MyViewController inside a third view controller. The problem is that I don't have navigation bar in MyViewController after presenting it. Can you help me?
我有系统“NavigationViewController -> MyViewController”,我想以编程方式在第三个视图控制器中显示 MyViewController。问题是我在呈现后在 MyViewController 中没有导航栏。你能帮助我吗?
var VC1 = self.storyboard.instantiateViewControllerWithIdentifier("MyViewController") as ViewController
self.presentViewController(VC1, animated:true, completion: nil)
回答by stefandouganhyde
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:
调用在现有导航堆栈之外以模态方式presentViewController
呈现视图控制器;它不包含在您的 UINavigationController 或任何其他控件中。如果你想让你的新视图控制器有一个导航栏,你有两个主要选择:
Option 1. Push the new view controller onto your existing navigation stack, rather than presenting it modally:
选项 1. 将新的视图控制器推送到您现有的导航堆栈上,而不是以模态方式呈现:
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:
选项 2. 将新的视图控制器嵌入到新的导航控制器中,并以模态方式呈现新的导航控制器:
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.
哪一个最适合你是一个人机界面设计问题,但通常很清楚什么最有意义。
回答by Maksim Kniazev
SWIFT 3
斯威夫特 3
let VC1 = self.storyboard!.instantiateViewController(withIdentifier: "MyViewController") as! MyViewController
let navController = UINavigationController(rootViewController: VC1)
self.present(navController, animated:true, completion: nil)
回答by Vinod Joshi
My navigation bar was not showing, so I have used the following method in Swift 2 iOS 9
我的导航栏没有显示,所以我在 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)
回答by Honey
The accepted answer is great. This is not answer, but just an illustration of the issue.
接受的答案很棒。这不是答案,而只是问题的说明。
I present a viewController like this:
我提出了一个这样的视图控制器:
inside vc1:
在 vc1 内:
func showVC2() {
if let navController = self.navigationController{
navController.present(vc2, animated: true)
}
}
inside vc2:
在 vc2 里面:
func returnFromVC2() {
if let navController = self.navigationController {
navController.popViewController(animated: true)
}else{
print("navigationController is nil") <-- I was reaching here!
}
}
As 'stefandouganhyde' has said: "it is not contained by your UINavigationController or any other"
正如“stefandouganhyde”所说:“它不包含在您的 UINavigationController 或任何其他控件中”
new solution:
新解决方案:
func returnFromVC2() {
dismiss(animated: true, completion: nil)
}
回答by Atka
I used an extension to UIViewController and a struct to make sure that my current view is presented from the favourites
我使用了 UIViewController 的扩展和结构来确保我当前的视图是从收藏夹中显示的
1.Struct for a global Bool
1.Struct 全局 Bool
struct PresentedFromFavourites {
static var comingFromFav = false}
2.UIVeiwController extension: presented modally as in the second option by "stefandouganhyde - Option 2 " and solving the back
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
}
}}