Elia Mirafiori
Elia Mirafiori

Reputation: 117

SWIFT 5 - navigation bar disappears after calling "show" method for changing storyboard

I'm new in Swift and I've this issue: I load the main storyboard and if the user is nil then I load another storyboard with other scenes. For doing this I run this code:

override func viewDidAppear(_ animated: Bool) {
    if (true) {
        let loginScene = UIStoryboard(name: "NilUser", bundle: nil).instantiateViewController(withIdentifier: "loginScene") as! LoginController
        loginScene.modalPresentationStyle = .fullScreen
        show(loginScene, sender: nil)
    }
}

I know that the if statement is always true, I want it at the moment. I also checked if the login scene was the initial one, but even like this the navigation bar doesn't show itself. The problem is that now ini the login scene I don't see the navigation bar and I don't get why. What can I do? If you need more explanations I'll give you. Thanks!

Upvotes: 0

Views: 763

Answers (2)

Daniel Marx
Daniel Marx

Reputation: 764

As far as I understand

  1. your main storyboard has no NavigationController therefore:
self.navigationController?.pushViewController(loginScene, animated:true)

won't work since self.navigationController? is nil

  1. Your NilUser Storyboard seems to have the loginScene (UIViewController) embedded in a UINavigationController I guess?!? and you expect this UINavigationController to be presented

by calling

let loginScene = UIStoryboard(name: "NilUser", bundle: nil).instantiateViewController(withIdentifier: "loginScene") as! LoginController

you instantiate your LoginController directly without it's UINavigationController therefore you should make the UINavigationController the initial ViewController and try

let storyboard = UIStoryboard(name: "NilUser", bundle: nil)
guard let navigationController = storyboard.instantiateInitialViewController() else {
    // initial UINavigationController not found
    return
}
navigationController.modalPresentationStyle = .fullScreen
show(navigationController, sender: nil)

If I'm wrong I might have missed something in your setup.

Upvotes: 1

shanth kumar
shanth kumar

Reputation: 25

try this

let loginScene = UIStoryboard(name: "NilUser", bundle: nil).instantiateViewController(withIdentifier: "loginScene") as! LoginController

   self.navigationController?.pushViewController(loginScene, animated:true)

Upvotes: 0

Related Questions