使用NavigationViewController swift呈现ViewController

时间:2014-08-22 09:51:17

标签: ios swift uinavigationcontroller uinavigationbar viewcontroller

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

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

5 个答案:

答案 0 :(得分:171)

调用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 :(得分:12)

我的导航栏没有显示,所以我在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 :(得分:12)

SWIFT 3

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

答案 3 :(得分:0)

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

1.构建全球Bool

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
    }

}}

答案 4 :(得分:0)

接受的答案很好。这不是答案,而只是问题的例证。

我提出了一个这样的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)
}