导航栏没有显示iOS swift [英] Navigation bar not showing iOS swift

查看:229
本文介绍了导航栏没有显示iOS swift的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

现在我想在其他一些viewController中添加导航栏但是,我的导航栏在该viewcontroller中不可见。为什么会这样?



我的故事板显示了导航栏但是一旦我尝试运行我的应用程序它就消失了。



如果我从一个视图控制器隐藏导航栏然后我们不能使用导航控制器,是这样吗?我希望我错了。那么导航栏没有显示的原因是什么?



编辑:



另外,我只希望我的视图控制器处于纵向模式。所以我做了以下是导致问题吗?

 扩展名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]
}


}

编辑1:



我使用以下代码从一个视图控制器移动而不是从故事板链接。这会引起问题吗?

 让storyboard = UIStoryboard(名称:Main,bundle:nil)
let secondViewController = storyboard.instantiateViewControllerWithIdentifier( HomeVC)
presentViewController(secondViewController,animated:false,completion:nil)

编辑2:



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





修改3



此处是我的导航控制器属性检查器

解决方案

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



NAV - > A - >(segue)B



即使你会隐藏导航栏,你仍然应该能够制作segues。你也不能在viewWillAppear的第二个(B)视图控制器中取消隐藏导航栏吗?首先,以相同的方式将其隐藏在viewWillAppear上。



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

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

而不是:

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

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


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

Edit 3:

Here is my navigation controller attribute inspector

解决方案

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

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.

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.

这篇关于导航栏没有显示iOS swift的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆