如何快速隐藏第一个 ViewController 或特定 ViewController 的导航栏?
我在 viewDidLoad()
中使用了以下代码:
override func viewDidLoad() {
super.viewDidLoad()
self.navigationController?.isNavigationBarHidden = true
}
还有在 viewWillAppear
上:
override func viewWillAppear(animated: Bool) {
self.navigationController?.isNavigationBarHidden = true
}
这两种方法都对所有 ViewController 隐藏了导航控制器。
如果您知道所有其他视图都应该使该栏可见,则可以使用 viewWillDisappear
将其再次设置为可见。
在斯威夫特:
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
navigationController?.setNavigationBarHidden(true, animated: animated)
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
navigationController?.setNavigationBarHidden(false, animated: animated)
}
斯威夫特 3
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
// Hide the navigation bar on the this view controller
self.navigationController?.setNavigationBarHidden(true, animated: animated)
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
// Show the navigation bar on other view controllers
self.navigationController?.setNavigationBarHidden(false, animated: animated)
}
self.
不需要。
您可以在 viewWillDisappear
中取消隐藏 navigationController
override func viewWillDisappear(animated: Bool)
{
super.viewWillDisappear(animated)
self.navigationController?.isNavigationBarHidden = false
}
斯威夫特 3
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
self.navigationController?.setNavigationBarHidden(false, animated: animated)
}
您还可以为此创建一个扩展,这样您就可以重用该扩展,而无需在每个视图控制器中一次又一次地实现它。
import UIKit
extension UIViewController {
func hideNavigationBar(animated: Bool){
// Hide the navigation bar on the this view controller
self.navigationController?.setNavigationBarHidden(true, animated: animated)
}
func showNavigationBar(animated: Bool) {
// Show the navigation bar on other view controllers
self.navigationController?.setNavigationBarHidden(false, animated: animated)
}
}
所以你可以使用如下的扩展方法
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
hideNavigationBar(animated: animated)
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
showNavigationBar(animated: animated)
}
在 Swift 3 中,您还可以使用 isNavigationBarHidden 属性来显示或隐藏导航栏
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
// Hide the navigation bar for current view controller
self.navigationController?.isNavigationBarHidden = true;
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
// Show the navigation bar on other view controllers
self.navigationController?.isNavigationBarHidden = false;
}
在 Swift 中隐藏导航栏的方法:
self.navigationController?.setNavigationBarHidden(true, animated: true)
self.navigationController?.navigationBar.isHidden = true
self.navigationController?.isNavigationBarHidden = true
在 Swift 中显示导航栏的方法:
self.navigationController?.setNavigationBarHidden(false, animated: true)
self.navigationController?.navigationBar.isHidden = false
self.navigationController?.isNavigationBarHidden = false
private func setupView() {
view.backgroundColor = .white
navigationController?.setNavigationBarHidden(true, animated: false)
}
选择
在 viewDidLoad 中使用这个设置
title = "Madman"
navigationController?.isNavigationBarHidden = false
navigationController?.navigationBar.prefersLargeTitles = true
navigationItem.largeTitleDisplayMode = .always
检查Collectionview、scrollview或tableView的约束
NSLayoutConstraint.activate([
tableView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
tableView.bottomAnchor.constraint(equalTo: view.bottomAnchor),
tableView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
tableView.trailingAnchor.constraint(equalTo: view.trailingAnchor)
])
/*. Swift 5 */
let controller = self.storyboard?.instantiateViewController(withIdentifier: "sc_userNavigation") as! UserNavigationViewController
let navigationController = UINavigationController(rootViewController: controller)
navigationController.setNavigationBarHidden(true, animated: false)
navigationController.modalPresentationStyle = .fullScreen
self.present(navigationController, animated: false, completion: nil)
在 IOS 8 中这样做
navigationController?.hidesBarsOnTap = true
但仅当它是 UINavigationController 的一部分时
当你想要它时让它成为假
我使用上述的变体,并将我的应用程序的部分隔离到嵌入不同的 NavController 中。这样,我不必重置可见性。例如,在启动序列中非常有用。
在视图中调用 set hide 方法会出现和消失。如果你不调用视图中的方法将消失,状态为 false。它将在完整的导航层次结构中隐藏导航栏
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.navigationController?.setNavigationBarHidden(true, animated: true)
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
self.navigationController?.setNavigationBarHidden(false, animated:true)
}
您可以从窗口控制器(Swift3)中做到这一点
class WindowController: NSWindowController {
override func windowDidLoad() {
super.windowDidLoad()
window?.titleVisibility = .hidden
}
}