简体   繁体   English

隐藏和显示导航栏时,导航栏后退按钮消失

[英]Navigation bar back button disappears when hiding and showing navigation bar

I'm trying to recreate a behaviour similar to the one seen for the UINavigationController in Apple News app. 我正在尝试重新创建类似于Apple News应用程序中针对UINavigationController看到的行为。
I have two controllers A and B, A is embedded in a UINavigationController and has a segue to B. 我有两个控制器A和B,A嵌入在UINavigationController中,并且与B相连。
The navigation bar in A is hidden while in B its visible and when moving from one to the other it animates correctly. A中的导航栏是隐藏的,而B中的导航栏是可见的,并且从一个移动到另一个时,它的动画效果正确。

To achieve this I'm setting A as the delegate for UINavigationController and adding 为此,我将A设置为UINavigationController的委托,并添加

func navigationController(_ navigationController: UINavigationController, animationControllerFor operation: UINavigationController.Operation, from fromVC: UIViewController, to toVC: UIViewController) -> UIViewControllerAnimatedTransitioning? {
    if operation == .push {
        navigationController.setNavigationBarHidden(false, animated: false)
    }
    return nil
}

func navigationController(_ navigationController: UINavigationController, willShow viewController: UIViewController, animated: Bool) {
    if let coordinator = navigationController.topViewController?.transitionCoordinator {
        if let _ = viewController as? ViewControllerA {
           navigationController.setNavigationBarHidden(true, animated: false)
        }

        coordinator.notifyWhenInteractionChanges { (context) in
            if context.isCancelled == true {
                navigationController.setNavigationBarHidden(false, animated: false)
            }
        }
    }
}

The issue I'm encountering is with the swipe back gesture. 我遇到的问题是向后滑动手势。
When the gesture starts I'm hiding the navigation bar in order for it to not be visible on ViewControllerA but if the gesture is cancelled I'm showing the bar again. 当手势开始时,我将隐藏导航栏,以使其在ViewControllerA上不可见,但是如果取消手势,则将再次显示该栏。
Unfortunately when showing it again, while the bar itself is visibile, the back button doesn't reappear for some reason. 不幸的是,当再次显示它时,尽管条本身是可见的,但由于某些原因后退按钮不会重新出现。
Do you know why is that? 你知道为什么吗

问题的一个例子

I think you do not have to work with the navigation controller delegate in this case but simply show / hide the navigation bar in the view controllers' viewWillAppear methods: 我认为您在这种情况下不必使用导航控制器委托,而只需在视图控制器的viewWillAppear方法中显示/隐藏导航栏:

class ViewControllerA: UIViewController {

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        navigationController?.setNavigationBarHidden(true, animated: animated)
    }

}

class ViewControllerB: UIViewController {

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        navigationController?.setNavigationBarHidden(false, animated: animated)
    }

}

I think this is the simpler solution that does not make any problems with missing back bar button items. 我认为这是较简单的解决方案,不会因缺少后退按钮而造成任何问题。 :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM