我有一个View Controller,这个View Controller在main.storybard中包含两个视图/场景。
我试图在第一个视图/场景中隐藏顶部导航栏,但在第二个视图/场景中再次取消隐藏它。
我尝试过

self.navigationController?.isNavigationBarHidden = true


但这仅适用于两个View Controller类。

有人有管理的想法吗?

最佳答案

viewWillAppear中隐藏导航栏,在viewWillDisappear中取消隐藏

var shouldHideNavBar = false

override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.navigationController?.setNavigationBarHidden(shouldHideNavBar, animated: animated)
}
override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        if shouldHideNavBar == true {
             navigationController?.setNavigationBarHidden(false, animated: animated)
        }
}

当您执行segue时,将shouldHideNavBar设置为true
override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
    if (segue.identifier == "show") {
        let viewController = segue!.destinationViewController as! ViewController
        viewController.shouldHideNavBar = true
    }
}

关于ios - Swift 4 One View Controller两个 View 隐藏导航栏,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/51767122/

10-13 03:54