0

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

Jacob
  • 121
  • 1
  • 8
  • Is `AdPostViewController` the identifier of the navigationController you want to show? – D. Mika Feb 07 '21 at 09:14
  • I think you should give an identifier to your nav controller and reference that rather than `AdPostViewController` otherwise it will load your view controller without the navController/navBar – Mat Feb 07 '21 at 10:46

2 Answers2

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.

zeytin
  • 5,545
  • 4
  • 14
  • 38
0

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.

D. Mika
  • 2,577
  • 1
  • 13
  • 29
  • your answer showed the navigation controller perfectly but not in a full screen, it showed it like popover @d-mika – Jacob Feb 07 '21 at 09:39
  • Add `vc.modalPresentationStyle = .fullScreen` or modify the storyboard accordingly. (In the attribute inspector, property *Presentation*) – D. Mika Feb 07 '21 at 10:06