skip to Main Content

I have a view controller with bar button item, and an action on this button to navigate from the view to another navigation view controller, but when I navigate to the navigation controller the navigation bar is hidden!

my navigation code

guard let window = UIApplication.shared.keyWindow else { return }
    let sb = UIStoryboard(name: "Main", bundle: nil)
    let vc = sb.instantiateViewController(identifier: "AdPostViewController")
    window.rootViewController = vc
    UIView.transition(with: window, duration: 0.5, options: .transitionCrossDissolve, animations: nil, completion: nil)

to clarify
I did not declare isNavigationBarhidden in the code, I am embedding a view controller to navigation controller, and when I navigate from the main view controller to the navigation controller I see the bar is hidden and I want to show it

2

Answers


  1. You can set isNavigationBarHidden. Apple documented as below:

    The default value is false. Setting this property changes the visibility of the navigation bar without animating the changes. If you want to animate the change, use the setNavigationBarHidden(_:animated:)method instead.

    Login or Signup to reply.
  2. I recommend not to change window.rootViewController but to present the view controller with:

    func present(_ viewControllerToPresent: UIViewController, 
        animated flag: Bool, 
      completion: (() -> Void)? = nil)
    

    Your code become:

            let sb = UIStoryboard(name: "Main", bundle: nil)
            let vc = sb.instantiateViewController(identifier: "AdPostViewController")
            vc.modalTransitionStyle = .crossDissolve
            self.present(vc, animated: true, completion: nil)
    

    Edit:
    And you should check if AdPostViewController is the identifier of the navigationController in your Storyboard.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search