[英]Set NavigationBar for Destination ViewController Only
I have a ViewController called SourceViewController
that is embedded in a NavigationController. 我有一个称为
SourceViewController
的ViewController,它嵌入在NavigationController中。
SourceViewController
segues to DestinationViewController
upon UITableViewCell
selection. 选择
UITableViewCell
SourceViewController
选择到DestinationViewController
。
I want to hide the navigation bar on SourceViewController
, but display it on DestinationViewController
in order to show the Back button. 我想在
SourceViewController
上隐藏导航栏,但在DestinationViewController
上显示它以显示“后退”按钮。
So, in SourceViewController
: 因此,在
SourceViewController
:
override func viewDidLoad() {
super.viewDidLoad()
self.navigationController?.navigationBar.hidden = true
}
And in DestinationViewController
: override func viewDidLoad() { super.viewDidLoad() 并且在
DestinationViewController
:覆盖func viewDidLoad(){super.viewDidLoad()
self.navigationController?.navigationBar.hidden = false
}
However, when I tap "Back" in DestinationViewController
to return to SourceViewController
, the navigationBar reappears in SourceViewController
但是,当我在
DestinationViewController
点击“返回”以返回到SourceViewController
,导航SourceViewController
将重新出现在SourceViewController
The next 'obvious' step would be to set navigationBar.hidden = false
in viewDidAppear
in SourceViewController
, however this smells for many reasons: mainly DRYness but also when returning to SourceViewController
, there is a delay in hiding the navigationBar
, and it is visible for a split second. 下一个“显而易见的”步骤是在
SourceViewController
viewDidAppear
中设置navigationBar.hidden = false
,但这有很多原因:主要是DRYness,而且在返回SourceViewController
,隐藏navigationBar
有所延迟,并且对于一瞬间。
How do I solve this problem? 我该如何解决这个问题?
Check ViewController lifecycle Looking to understand the iOS UIViewController lifecycle . 检查ViewController生命周期希望了解iOS UIViewController生命周期 。 When you start the program viewDidLoad is called and everything is ok, but when you go back from detailController, viewDidLoad is not called, just change this line (self.navigationController?.navigationBar.hidden = true) in viewWillApear and everything must be ok.
当您启动程序时,将调用viewDidLoad并一切正常,但是从detailController返回时,未调用viewDidLoad,只需在viewWillApear中更改此行(self.navigationController?.navigationBar.hidden = true),一切都必须正常。
I think this will work, hiding the navigation bar. 我认为这会起作用,隐藏导航栏。 before appearing/disappearing the view.
在显示/消失视图之前。
override func viewWillAppear(animated: Bool) {
navigationController?.navigationBarHidden = true
super.viewWillAppear(animated)
}
override func viewWillDisappear(animated: Bool) {
navigationController?.navigationBarHidden = true
super.viewWillDisappear(animated)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.