Swift 3 - Why is my Navigation Bar not showing?

二次信任 提交于 2020-01-11 12:09:28

问题


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
}

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?


回答1:


You are showing vcString is your LoginVC ,

you need to use NavigationController identifier like loginView or

You need to embded in NavigationControler before show

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

    let navi =  UINavigationController.init(rootViewController: initialVC)

and

  window?.rootViewController = navi


来源:https://stackoverflow.com/questions/45407806/swift-3-why-is-my-navigation-bar-not-showing

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!