美文网首页iOS Developer
swift-自定义导航栏后侧滑返回功能失效解决办法(不用再添加侧

swift-自定义导航栏后侧滑返回功能失效解决办法(不用再添加侧

作者: iOS开发周立贺 | 来源:发表于2017-05-03 09:42 被阅读158次

从iOS7开始,系统为UINavigationController提供了一个interactivePopGestureRecognizer用于右滑返回(pop),但是如果自定了当前视图控制器leftBarButtonItem,该手势就失效了。

解决方法:自定义UINavigationController并实现其代理方法重新设置侧滑手势的代理。

具体代码如下:

class BaseNavigationController: UINavigationController ,UINavigationControllerDelegate {
    
    var popDelegate: UIGestureRecognizerDelegate?
    
    override func viewDidLoad() {
        
        super.viewDidLoad()
        
        self.popDelegate = self.interactivePopGestureRecognizer?.delegate
        
        self.delegate = self
    }
    
    // MARK: - UINavigationControllerDelegate方法
    func navigationController(_ navigationController: UINavigationController, didShow viewController: UIViewController, animated: Bool) {

        if viewController == self.viewControllers[0] {
            
            self.interactivePopGestureRecognizer!.delegate = self.popDelegate
        }
        else {
            self.interactivePopGestureRecognizer!.delegate = nil
        }
    }

相关文章

网友评论

    本文标题:swift-自定义导航栏后侧滑返回功能失效解决办法(不用再添加侧

    本文链接:https://www.haomeiwen.com/subject/fjlktxtx.html