简体   繁体   English

设置NavigationBar仅用于目的地ViewController

[英]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.

相关问题 在没有视觉错误的下一个 ViewController 上设置 NavigationBar 标题 - Set NavigationBar title on next ViewController without visual bugs 使用 new UINavigationBarAppearance() API 在单个 ViewController 上设置 navigationBar 颜色? - Use new UINavigationBarAppearance() API to set navigationBar color on a single ViewController? 如何设置NavigationBar与iOS11中的ViewController内容重叠 - How to set NavigationBar overlapping the ViewController content in iOS11 NavigationBar 覆盖的 ViewController - ViewController covered up by NavigationBar 在 Storyboard 中隐藏一个 ViewController 的 NavigationBar - Hide NavigationBar for one ViewController in Storyboard ios-不向ViewController上的NavigationBar添加backbutton - ios - Not adding backbutton to NavigationBar on ViewController 用NavigationBar替换TabbarController中的一个ViewController - Replace one ViewController in TabbarController with NavigationBar UINavigationController推送带有隐藏导航栏的viewcontroller - UINavigationController push viewcontroller with hidden navigationbar 如何以编程方式呈现带有导航栏的 ViewController? - How to present a ViewController with a Navigationbar programatically? 在目标viewController上设置Delegates - setting Delegates on destination viewController
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM