导航栏未显示 iOS swift

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33689255/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 08:13:45  来源:igfitidea点击:

Navigation bar not showing iOS swift

iosswiftuinavigationcontroller

提问by Amsheer

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

我的应用程序中有多个视图控制器。我想隐藏navigationbar在我的第一个视图控制器中。所以我使用下面的代码来隐藏导航栏

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:

编辑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:

编辑2:

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

请检查我的以下屏幕截图。哪些是我对 secondview 控制器的设置

enter image description here

在此处输入图片说明

enter image description here

在此处输入图片说明

Edit 3:

编辑3:

Here is my navigation controller attribute inspector enter image description here

这是我的导航控制器属性检查器 在此处输入图片说明

回答by Makalele

Navigation Controller is a controller, which has stack of view controllers. So if you have something like this:

导航控制器是一个控制器,它有一堆视图控制器。所以如果你有这样的事情:

NAV -> A -> (segue) B

NAV -> A -> (segue) B

Even if you'll hide navigation bar you still should be able to make segues. Also can't you just unhide navigation bar in second (B) view controller in viewWillAppear? And in first the same way hide it on viewWillAppear.

即使您将隐藏导航栏,您仍然应该能够进行转场。你也不能在viewWillAppear的第二个(B)视图控制器中取消隐藏导航栏吗?首先以同样的方式将它隐藏在 viewWillAppear 上。

edit: Final solution to the problem: Use:

编辑:问题的最终解决方案:使用:

 let controller = storyboard.instantiateViewControllerWithIdentifier("HomeVC")
 self.navigationController!.pushViewController(controller) 

instead of:

代替:

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

Because pushViewController will add secondViewController to its stack. presentViewController was replacing your navigation controller that's why you couldn't see navigation bar.

因为 pushViewController 会将 secondViewController 添加到其堆栈中。presentViewController 正在替换您的导航控制器,这就是您看不到导航栏的原因。

回答by Anbu.Karthik

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

    // Hide the navigation bar on the this view controller
    self.navigationController?.setNavigationBarHidden(true, animated: animated)
}

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)

    // Show the navigation bar on other view controllers
    self.navigationController?.setNavigationBarHidden(false, animated: animated)
}

回答by harsha yarabarla

in viewDidLoad method of the view controller in which you don't want to show navigation bar add the line

在您不想显示导航栏的视图控制器的 viewDidLoad 方法中添加行

navigationController.navigationBarHidden = true

you are presently hiding in all view controllers

您目前隐藏在所有视图控制器中

Edit: You are presenting view controller instead it should be

编辑:您正在呈现视图控制器,而不是它应该是

self.navigationController!.pushViewController(controller) 

回答by Vittal Pai

do like in viewcontrollerbased hidden using Swift 4.0

喜欢在viewcontroller使用Swift 4.0 的基础隐藏

To hide navigationControllerin viewWillAppear

要隐藏navigationControllerviewWillAppear

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

To unhide navigationControllerin viewWillDisappear

要隐藏navigationControllerviewWillDisappear

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

回答by swiftBoy

I am having same requirement in my swift project.

我在我的 swift 项目中有同样的要求。

this is how I have handled Navigation bar

这就是我处理导航栏的方式

Make sure your first screen is embedded into Navigation controller

确保您的第一个屏幕嵌入到导航控制器中

enter image description here

在此处输入图片说明

example we have two screens A and B

例如我们有两个屏幕 A 和 B

In screen A you need to hide navigation bar in viewWillAppear

在屏幕 A 中,您需要在viewWillAppear 中隐藏导航栏

override func viewWillAppear(animated: Bool)
    {
         super.viewWillAppear(animated)

        //hide navigation for screen A
        self.navigationController?.navigationBarHidden = true
    }

for enabling Navigation in screen B you need to add below code in screen A

要在屏幕 B 中启用导航,您需要在屏幕 A 中添加以下代码

override func prepareForSegue(segue: (UIStoryboardSegue!), sender: AnyObject!)
    {
        if (segue.identifier == "screen B's segue identifier here")
        {
           //enable navigation for screen B       
            navigationController?.setNavigationBarHidden(navigationController?.navigationBarHidden == false, animated: true)
        }

    }

Using above style, I can enable or disable navigation bar for specific screen, whenever I want

使用上述样式,我可以随时启用或禁用特定屏幕的导航栏

回答by sunshinejr

If you need to have this navigation bar hidden only in this controller, the best way is to show it in viewWillDisappear()and hide in viewWillAppear().

如果你只需要在这个控制器中隐藏这个导航栏,最好的方法是viewWillDisappear()viewWillAppear().

回答by IPL10

It's too late to reply and there are other good answers but I would like to share what worked for me.

现在回复为时已晚,还有其他好的答案,但我想分享对我有用的内容。

let controller = self.storyboard?.instantiateViewControllerWithIdentifier("HomeVC")
    self.navigationController!.pushViewController(controller!, animated:true)