Swift 3 - 为什么我的导航栏没有显示? [英] Swift 3 - Why is my Navigation Bar not showing?

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

问题描述

我有一个简单的应用程序来测试rest api的推送通知。我想在应用程序中显示导航栏但它不起作用。在我的AppDelegate中,我有以下代码:

I have a simple app to test push notifications of a rest api. I would like to show the Navigation Bars in the App but it is not working. In my AppDelegate I have the following code:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    self.registerForPushNotifications()
    let url = "dev"
    UserDefaults.standard.setValue(FFHelper.url(slug: url.slug()), forKey: "api-url")
    var vcString = "loginView"
    if KeychainSwift().get("auth-token") != nil {
        vcString = "notificationsTable"
    }
    let initialVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: vcString)
    initialVC.navigationController?.navigationBar.isHidden = false
    initialVC.navigationController?.setNavigationBarHidden(false, animated: true)
    window?.rootViewController = initialVC
    window?.makeKeyAndVisible()
    return true
}

在故事板中,导航栏也启用:

最后,在viewController中,我做了同样的事情:

In the Storyboard the navigation bar is enabled also: Lastly, in the viewController, I do the same thing:

override func viewWillAppear(_ animated: Bool) {
    self.navigationController?.setNavigationBarHidden(false, animated: animated)
    self.navigationController?.navigationBar.isHidden = false
    super.viewWillAppear(animated)
}

但是在启动应用程序后,导航栏仍未显示:

有人可以告诉我为什么会这样?

But still after launching the app, the navigation bar ist not showing: Can someone please tell me why this is?

推荐答案

您正在显示vcString是您的 LoginVC

You are showing vcString is your LoginVC ,

您需要使用NavigationController标识符,例如 loginView

you need to use NavigationController identifier like loginView or

你需要在show之前嵌入 NavigationControler

You need to embded in NavigationControler before show

let initialVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: vcString)

    let navi =  UINavigationController.init(rootViewController: initialVC)

  window?.rootViewController = navi

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

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