setNavigationBarHidden 无法以编程方式工作?

uni*_*ise 2 ios swift swift3

我正在尝试以编程方式创建不同的视图控制器,其中第一个不应显示导航栏,但第二个应该显示。我似乎无法做任何事情来让第二个视图控制器显示导航栏。所有代码都可以正常编译,并且推动控制器的按钮可以工作,因为第二个屏幕会按预期变为绿色。

这是 AppDelegate:

class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.

    window = UIWindow(frame: UIScreen.main.bounds)
    window?.makeKeyAndVisible()

    let layout = UICollectionViewFlowLayout()
    let startupScreenController = StartupScreenController(collectionViewLayout: layout)
    window?.rootViewController = UINavigationController(rootViewController: startupScreenController)

    application.statusBarStyle = .lightContent

    return true
}
Run Code Online (Sandbox Code Playgroud)

这是第一个视图控制器:

func skipButtonPressed() {

    let layout = UICollectionViewFlowLayout()
    let secondViewController = SecondViewController(collectionViewLayout: layout)
    self.navigationController?.pushViewController(secondViewController, animated: true)

}

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

    // Hide the navigation bar on this view controller
    self.navigationController?.setNavigationBarHidden(true, animated: animated)
}
Run Code Online (Sandbox Code Playgroud)

这是第二个视图控制器:

class SecondViewController: UICollectionViewController {

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)

    // Show the Navigation Bar on this view controller

    self.navigationController?.setNavigationBarHidden(false, animated: animated)
    UINavigationBar.appearance().barTintColor = UIColor(red: 24/255, green: 24/255, blue: 24/255, alpha: 1  )
}

override func viewDidLoad() {
    super.viewDidLoad()

    collectionView?.backgroundColor = UIColor.green

}
Run Code Online (Sandbox Code Playgroud)

7Re*_*com 10

对于我的项目,只有当我将其添加到 viewWillLayoutSubviews() 中时它才起作用。

override func viewWillLayoutSubviews() {
    super.viewWillLayoutSubviews()

    self.navigationController?.navigationBar.isHidden = true
}
Run Code Online (Sandbox Code Playgroud)