昨天接到新的修改需求,要求有的页面不能返回,必须强制用户往下走流程,当时一听差点给跪啦!无奈啊,谁叫我是干开发的,改吧!初步一想,把返回按钮关掉就OK啦,确实返回按钮关掉就不能按返回按钮返回上一页啦,窃喜!!!后来发现还有边缘返回,这下头疼了,只关掉按钮明显达不到效果,于是赶紧求助小伙伴,各种支招,真是非常的感谢他们,一一试过,发现达不到效果。无奈之下,自己尝试,历经磨难,终于成功,现在献上代码,希望对大家有帮助!写的不好的地方尽请见谅,本人第一次写!
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
self.isCanSideBack = NO;
//关闭ios右滑返回
if([self.navigationControllerrespondsToSelector:@selector(interactivePopGestureRecognizer)]) {
self.navigationController.interactivePopGestureRecognizer.delegate=self;
}
}
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer*)gestureRecognizer {
return self.isCanSideBack;
}
之前的写法,测试之后发现有bug,如果是直接调用[self.navigationController popToRootViewControllerAnimated:YES];
不会走viewDidDisappear里的逻辑,离开该页以后,右滑返回失效了,所以做了如下的修改,在离开页面的方法里,调用[self resetSideBack];
即可保证只是当前页的右滑返回被关闭,其他页面的右滑返回依然正常!
- (void)viewDidDisappear:(BOOL)animated {
[superviewDidDisappear:animated];
[self resetSideBack];
}
/**
*恢复边缘返回
*/
- (void)resetSideBack {
self.isCanSideBack=YES;
//开启ios右滑返回
if([self.navigationControllerrespondsToSelector:@selector(interactivePopGestureRecognizer)]) {
self.navigationController.interactivePopGestureRecognizer.delegate = nil;
}
}
补充说明:实现类需要继承
<UIGestureRecognizerDelegate>
协议,而- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer*)gestureRecognizer
是系统自己调用的!
网友评论
UIPanGestureRecognizer * pan = [[UIPanGestureRecognizer alloc]initWithTarget:traget action:nil];
pan.cancelsTouchesInView = NO;
pan.delaysTouchesEnded = NO;
[self.view addGestureRecognizer:pan];
id traget = self.navigationController.interactivePopGestureRecognizer.delegate;
UIPanGestureRecognizer * pan = [[UIPanGestureRecognizer alloc]initWithTarget:traget action:nil];
[self.view addGestureRecognizer:pan];
直接将代码拷贝到viewDidLoad总就行了,我是深深的买醉了
//开启ios右滑返回
if([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
self.navigationController.interactivePopGestureRecognizer.delegate = self;
//这里应该是 self 做代理
}
- (void)dealloc{
self.navigationController.delegate = nil;
}
//在 dealloc 中给 delegate置nil
ios10 自带输入法,输入两个拼音时,不显示汉字,显示....求解这个问题如何解决
self.navigationController.interactivePopGestureRecognizer.enabled = NO;
让系统左滑失效