Ios setNavigationBarHidden未以编程方式工作?
我试图以编程方式创建不同的视图控制器,其中第一个视图控制器不应显示导航栏,而第二个视图控制器应显示导航栏。我似乎无法让第二个视图控制器显示导航栏。所有的代码编译得很好,按下控制器的按钮工作,因为第二个屏幕会变成绿色 以下是AppDelegate:Ios setNavigationBarHidden未以编程方式工作?,ios,swift,swift3,Ios,Swift,Swift3,我试图以编程方式创建不同的视图控制器,其中第一个视图控制器不应显示导航栏,而第二个视图控制器应显示导航栏。我似乎无法让第二个视图控制器显示导航栏。所有的代码编译得很好,按下控制器的按钮工作,因为第二个屏幕会变成绿色 以下是AppDelegate: class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? func application(_ application: UIApplication,
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
}
这是第二个视图控制器:
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
}
您可以直接在ViewDidLoad中使用该导航条形码
override func viewDidLoad() {
self.navigationController?.setNavigationBarHidden(false, animated:
animated)
}
你必须像下面这样做
func viewDidLoad() {
super.viewDidLoad()
self.navigationController?.setNavigationBarHidden(false, animated: animated)
}
按以下方式隐藏
导航栏
。设置false
以启用返回
override func viewDidLoad() {
super.viewDidLoad()
self.navigationController?.navigationBar.isHidden = true
}
对于我的项目,它只有在我将其添加到viewWillLayoutSubviews()中时才起作用
别忘了添加call
super.viewDidLoad()
根据stackoverflow指南,您应该接受正确答案,以便其他人在遇到相同问题时获得帮助@unicorn_惊讶这不会编译,因为动画
没有在任何地方声明。