美文网首页
自定义返回按钮后恢复滑动返回

自定义返回按钮后恢复滑动返回

作者: Dolway | 来源:发表于2020-04-27 11:36 被阅读0次

如相信各位做项目的,一般都是用的自定义的返回按钮,所以我贴几行代码,看看怎么实现系统自带的滑动返回的。
首先,建立一个自定义的返回按钮,然后加上去

  [self.navigationItem setLeftBarButtonItem:backBarItem];

然后

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    //代理置空,否则会闪退,试了几台设备好像不置空也没问题。
   //有些博客中的方案更保守:设置代理之前先获取原代理并保存。在此处还原为原代理
    if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
    self.navigationController.interactivePopGestureRecognizer.delegate = nil;
}
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
   //开启iOS7的滑动返回效果
    if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
    //只有在二级页面生效
    if ([self.navigationController.viewControllers count] == 2) {
           self.navigationController.interactivePopGestureRecognizer.delegate = self;
    }
}
}

相关文章

网友评论

      本文标题:自定义返回按钮后恢复滑动返回

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