【发布时间】:2017-07-13 05:48:47
【问题描述】:
我正在尝试以编程方式创建不同的视图控制器,其中第一个不应该显示导航栏,但第二个应该。我似乎无法做任何事情来让第二个视图控制器显示导航栏。所有代码都编译得很好,推动控制器的按钮也能正常工作,因为第二个屏幕应该变绿了。
这是 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
}
这是第一个视图控制器:
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)
}
这是第二个视图控制器:
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
}
【问题讨论】: