ios 如何在 Swift 中的第一个 ViewController 中隐藏导航栏?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29209453/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to hide a navigation bar from first ViewController in Swift?
提问by Aby Mathew
How can I hide a navigation bar from first ViewController or a particular ViewController in swift?
如何快速隐藏第一个 ViewController 或特定 ViewController 的导航栏?
I used the following code in viewDidLoad()
:
我在以下代码中使用了viewDidLoad()
:
override func viewDidLoad() {
super.viewDidLoad()
self.navigationController?.isNavigationBarHidden = true
}
and also on viewWillAppear
:
还有viewWillAppear
:
override func viewWillAppear(animated: Bool) {
self.navigationController?.isNavigationBarHidden = true
}
Both methods hide the navigation controller from all ViewControllers.
这两种方法都隐藏了所有 ViewController 的导航控制器。
回答by Rengers
If you know that all other views should have the bar visible, you could use viewWillDisappear
to set it to visible again.
如果您知道所有其他视图都应该使栏可见,则可以使用viewWillDisappear
将其再次设置为可见。
In Swift:
在斯威夫特:
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)
}
回答by Michael Garito
Swift 3
斯威夫特 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)
}
回答by Deepesh
You can unhide navigationController
in viewWillDisappear
您可以取消隐藏navigationController
在viewWillDisappear
override func viewWillDisappear(animated: Bool)
{
super.viewWillDisappear(animated)
self.navigationController?.isNavigationBarHidden = false
}
Swift 3
斯威夫特 3
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
self.navigationController?.setNavigationBarHidden(false, animated: animated)
}
回答by Ankahathara
You could also create an extension for this so you will be able to reuse the extension without implementing this again and again in every view controller.
您还可以为此创建一个扩展,这样您就可以重用该扩展,而无需在每个视图控制器中一次又一次地实现它。
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)
}
}
So you can use the extension methods as below
所以你可以使用下面的扩展方法
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
hideNavigationBar(animated: animated)
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
showNavigationBar(animated: animated)
}
回答by Dilip Jangid
In Swift 3, you can use isNavigationBarHidden Property also to show or hide navigation bar
在 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;
}
回答by Mahesh Chaudhari
Ways to hide Navigation Bar in Swift:
在 Swift 中隐藏导航栏的方法:
self.navigationController?.setNavigationBarHidden(true, animated: true)
self.navigationController?.navigationBar.isHidden = true
self.navigationController?.isNavigationBarHidden = true
回答by Mahesh Chaudhari
Ways to show Navigation Bar in Swift:
在 Swift 中显示导航栏的方法:
self.navigationController?.setNavigationBarHidden(false, animated: true)
self.navigationController?.navigationBar.isHidden = false
self.navigationController?.isNavigationBarHidden = false
回答by Wasim
private func setupView() {
view.backgroundColor = .white
navigationController?.setNavigationBarHidden(true, animated: false)
}
回答by Talha Rasool
Call the set hide method in view Will appear and Disappear. if you will not call the method in view will disappear with status false.It will hide the navigation bar in complete navigation hierarchy
在view中调用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)
}
回答by Jawad Zeb
In IOS 8 do it like
在 IOS 8 中这样做
navigationController?.hidesBarsOnTap = true
but only when it's part of a UINavigationController
但仅当它是UINavigationController的一部分时
make it false when you want it back
当你想要它回来时让它成为假