从条形按钮项导航到导航控制器

Navigate from Bar Button item to navigation controller

我有一个带有条形按钮项的视图控制器,以及一个从视图导航到另一个导航视图控制器的按钮上的操作,但是当我导航到导航控制器时,导航栏被隐藏了!

我的导航码

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)

澄清一下 我没有在代码中声明 isNavigationBarhidden,我将视图控制器嵌入到导航控制器,当我从主视图控制器导航到导航控制器时,我看到该栏被隐藏,我想显示它

您可以设置isNavigationBarHidden。苹果记录如下:

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.

我建议不要更改 window.rootViewController,而是向视图控制器显示:

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

您的代码变为:

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

编辑: 你应该检查 AdPostViewController 是否是 Storyboard 中 navigationController 的标识符。