我正在尝试以编程方式创建不同的视图控制器,其中第一个不应显示导航栏,但第二个应该显示。我似乎无法做任何事情让第二个视图控制器显示导航栏。所有代码编译都很好,按钮推动控制器工作,因为第二个屏幕变为绿色。
这是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
}
这是第一个View Controller:
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)
}
这是第二个View Controller:
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
}
答案 0 :(得分:1)
以下列方式隐藏navigation bar
。设置false
以启用后退
override func viewDidLoad() {
super.viewDidLoad()
self.navigationController?.navigationBar.isHidden = true
}
答案 1 :(得分:0)
您可以直接使用ViewDidLoad中的导航条形码。
override func viewDidLoad() {
self.navigationController?.setNavigationBarHidden(false, animated:
animated)
}
答案 2 :(得分:0)
你必须这样做。
func viewDidLoad() {
super.viewDidLoad()
self.navigationController?.setNavigationBarHidden(false, animated: animated)
}
答案 3 :(得分:0)
对于我的项目,仅当将其添加到viewWillLayoutSubviews()中时,该项目才能起作用。
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
self.navigationController?.navigationBar.isHidden = true
}