Ios 导航控制器问题(一):关闭导航栏默认的手势侧滑返回功能

方法是让当前控制器取代导航的手势代理,控制器消失时再还回去,略麻烦但无副作用

// 设置属性保存导航的interactivePopGestureRecognizer的代理,记得使用weak标记弱引用
 weak var savedGestureRecognizerDelegate:UIGestureRecognizerDelegate?

// 在进入控制器时保存导航的interactivePopGestureRecognizer属性
override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        savedGestureRecognizerDelegate = self.navigationController?.interactivePopGestureRecognizer?.delegate
        self.navigationController?.interactivePopGestureRecognizer?.delegate = self
    }

//离开控制器的时候,把导航的delegate还回去
  override func viewWillDisappear(_ animated: Bool) {
        self.navigationController?.interactivePopGestureRecognizer?.delegate = savedGestureRecognizerDelegate
    }

//遵守UIGestureRecognizerDelegate协议,把手势返回的代理方法实现为空
func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
        if gestureRecognizer == self.navigationController?.interactivePopGestureRecognizer {
            return false
        }
        return true
    }

你可能感兴趣的:(Ios 导航控制器问题(一):关闭导航栏默认的手势侧滑返回功能)