Swift 导航全屏滑动返回实现

import UIKit

fileprivate var slideEnabledKey = "slideEnabled"

extension UINavigationController {
    /// should slide to back. default is true
    var slideEnabled: Bool {
        set {
            objc_setAssociatedObject(self, &slideEnabledKey, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_ASSIGN)
        }
        get {
            if let rs = objc_getAssociatedObject(self, &slideEnabledKey) as? Bool {
                return rs
            }
            return true
        }
    }
}

/// the navigationController enabled full screen slide to back
class FullNavigationController: UINavigationController, UIGestureRecognizerDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()
        fullSlide()
    }

    func fullSlide() {
        guard let recognizer = interactivePopGestureRecognizer else {
            return
        }
        guard let recognizerView = recognizer.view else {
            return
        }

        // use runtime to check the property name
//        var count: UInt32 = 0
//        let ivars = class_copyIvarList(UIGestureRecognizer.self, &count)!
//        for i in 0.. 0 {
            viewController.hidesBottomBarWhenPushed = true

        }
        super.pushViewController(viewController, animated: animated)
    }

    func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
        return slideEnabled && childViewControllers.count > 1
    }
}

你可能感兴趣的:(Swift 导航全屏滑动返回实现)