用NavigationViewController swift显示ViewController


75

我有系统“ NavigationViewController-> MyViewController”,我以编程方式希望在第三个视图控制器中显示MyViewController。问题是在显示它后,MyViewController中没有导航栏。你能帮助我吗?

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

Answers:


213

调用presentViewController模态方式在现有导航堆栈之外显示视图控制器;它不包含在您的UINavigationController或其他任何对象中。如果希望新视图控制器具有导航栏,则有两个主要选项:

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

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

选项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)

请记住,此选项不会自动包含“后退”按钮。您必须自己建立一个紧密的机制。

哪个是最适合您的是人机界面设计问题,但通常清楚什么才是最有意义的。


1
我怎样才能使新的视图控制器从右到左,而不是从下到上?谢谢
vincwng '16

4
好。但是我怎样才能关闭模态?
Peter Kreinz '16

MyViewController(ViewController)已通过StoryBoard链接到NavigationController。然后,选项1,不起作用,它当前的导航控制器没有导航栏!
kiran

如果看到双重NavigationBar,请编写此行代码。navController.isNavigationBarHidden = true
Nij

15

SWIFT 3

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

13

我的导航栏未显示,因此我在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)

2

公认的答案是伟大的。这不是答案,而只是问题的例证。

我提出这样一个viewController:

在vc1内部:

func showVC2() {
    if let navController = self.navigationController{
        navController.present(vc2, animated: true)
    }
}

在vc2内部:

func returnFromVC2() {
    if let navController = self.navigationController {
        navController.popViewController(animated: true)
    }else{
        print("navigationController is nil") <-- I was reaching here!
    }
}

正如'stefandouganhyde'所说:“您的UINavigationController或任何其他都不包含它”

新解决方案:

func returnFromVC2() {
    dismiss(animated: true, completion: nil)
}

1

我使用了UIViewController的扩展和结构来确保从收藏夹中显示当前视图

1.构建全球布尔

struct PresentedFromFavourites {
static var comingFromFav = false}

2.UIVeiwController扩展:由“ stefandouganhyde-选项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
    }

}}
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.