Swift 4 One View Controller两个视图隐藏导航栏

时间:2018-08-09 12:31:44

标签: ios swift uitableview user-interface uiview

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

self.navigationController?.isNavigationBarHidden = true

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

有人有管理想法吗?

2 个答案:

答案 0 :(得分:0)

id value userid1 1 userid1 2 userid1 3 . . . . userid2 1 中隐藏导航栏,在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) } } 设置为shouldHideNavBar

true

答案 1 :(得分:0)

您应该使用额外的控件。或者,您可以创建一个IBInspactable变量,并在Interface Builder上设置其值。像这样:

@IBDesignable class myViewController: UIViewController{

    @IBInspectable var isNavbarHidden: Bool = true{
        didSet{
            self.navigationController?.isNavigationBarHidden = isNavBarHidden
        }
    }


    override func viewDidLoad(){
        super.viewDidLoad()
        //I am not sure if this line is necessary
        self.navigationController?.isNavigationBarHidden = isNavBarHidden
    }
}

然后转到InterfaceBuilder(您的情节提要文件),并在viewControllers属性中为场景设置其值。