No Swipe Back when hiding Navigation Bar in UINavigationController

前端 未结 18 715
生来不讨喜
生来不讨喜 2020-12-04 06:34

I love the swipe pack thats inherited from embedding your views in a UINavigationController. Unfortunately i cannot seem to find a way to hide the Naviga

18条回答
  •  一个人的身影
    2020-12-04 07:10

    Building off of Hunter Maximillion Monk's answer, I made a subclass for UINavigationController and then set the custom class for my UINavigationController in my storyboard. Final code for the two classes looks like this:

    InteractivePopRecognizer:

    class InteractivePopRecognizer: NSObject {
    
        // MARK: - Properties
    
        fileprivate weak var navigationController: UINavigationController?
    
        // MARK: - Init
    
        init(controller: UINavigationController) {
            self.navigationController = controller
    
            super.init()
    
            self.navigationController?.interactivePopGestureRecognizer?.delegate = self
        }
    }
    
    extension InteractivePopRecognizer: UIGestureRecognizerDelegate {
        func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
            return (navigationController?.viewControllers.count ?? 0) > 1
        }
    
        // This is necessary because without it, subviews of your top controller can cancel out your gesture recognizer on the edge.
        func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
            return true
        }
    }
    

    HiddenNavBarNavigationController:

    class HiddenNavBarNavigationController: UINavigationController {
    
        // MARK: - Properties
    
        private var popRecognizer: InteractivePopRecognizer?
    
        // MARK: - Lifecycle
    
        override func viewDidLoad() {
            super.viewDidLoad()
            setupPopRecognizer()
        }
    
        // MARK: - Setup
    
        private func setupPopRecognizer() {
            popRecognizer = InteractivePopRecognizer(controller: self)
        }
    }
    

    Storyboard:

    Storyboard nav controller custom class

提交回复
热议问题