Swift 3 - Why is my Navigation Bar not showing?

2019-03-06 05:45发布

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: enter image description here 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: enter image description here Can someone please tell me why this is?

标签: ios swift swift3
1条回答
Rolldiameter
2楼-- · 2019-03-06 06:21

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
查看更多
登录 后发表回答