Navigation bar not showing iOS swift

2019-04-19 08:42发布

I am having multiple view controller in my application. I want to hide navigationbar in my first view controller. So I use the following code to hide the navigation bar

navigationController?.setNavigationBarHidden(navigationController?.navigationBarHidden == false, animated: true);

Now I want to add navigation bar in some other viewController but, my navigation bar not visible in that viewcontroller. Why it is happening?

My storyboard showing the navigation bar but once I try to run my application it is gone.

If I hide navigation bar from one view controller then we can't use navigation controller, Is it so? I hope I am wrong. Then what are the reasons for navigation bar not shown?

EDIT:

Also I want my view controller in portrait mode only. So I did the following Is that causing the issue?

extension UINavigationController{
    public override func shouldAutorotate() -> Bool {
        if (UIDevice.currentDevice().orientation == UIDeviceOrientation.LandscapeLeft ||
            UIDevice.currentDevice().orientation == UIDeviceOrientation.LandscapeRight ||
            UIDevice.currentDevice().orientation == UIDeviceOrientation.Unknown) {
                return false
        }
        else {
            return true
        }
    }

    public override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
        return [UIInterfaceOrientationMask.Portrait ,UIInterfaceOrientationMask.PortraitUpsideDown]
    }


}

Edit 1:

I am using following code to move from one view controller not link from the storyboard. Is that causing issue now?

 let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let secondViewController = storyboard.instantiateViewControllerWithIdentifier("HomeVC")
        presentViewController(secondViewController, animated: false, completion: nil)

Edit 2:

Please check my following screenshots. Which are my settings for secondview controller

enter image description here

enter image description here

Edit 3:

Here is my navigation controller attribute inspector enter image description here

7条回答
Deceive 欺骗
2楼-- · 2019-04-19 09:34

do like in viewcontroller based hidden using Swift 4.0

To hide navigationController in viewWillAppear

override func viewWillAppear(animated: Bool) {
     super.viewWillAppear(animated)
    self.navigationController?.isNavigationBarHidden = true
}

To unhide navigationController in viewWillDisappear

override func viewWillDisappear(animated: Bool)
{
    super.viewWillDisappear(animated)
    self.navigationController?.isNavigationBarHidden = false
}
查看更多
登录 后发表回答