iOS中自定义导航栏按钮的系统返回手势

iOS开发中,我们常常会自定义导航栏的按钮,这时候返回并不能滑动,在这里处理一下就行了,代码如下:


- (void)addPanGesture{

//setp1:需要获取系统自带滑动手势的target对象

idtarget =self.navigationController.interactivePopGestureRecognizer.delegate;

//setp2:创建全屏滑动手势~调用系统自带滑动手势的target的action方法

UIPanGestureRecognizer*pan = [[UIPanGestureRecognizeralloc]initWithTarget:targetaction:@selector(handleNavigationTransition:)];

//step3:设置手势代理~拦截手势触发

pan.delegate=self;

//step4:别忘了~给导航控制器的view添加全屏滑动手势

[self.viewaddGestureRecognizer:pan];

//step5:将系统自带的滑动手势禁用

self.navigationController.interactivePopGestureRecognizer.enabled=NO;

}

在代理方法中返回YES或NO


- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer*)gestureRecognizer

return NO;则不需要触发滑动手势

return YES;则需要触发滑动手势

PS:自定义图片,要么改图片尺寸,要么放在button上面,调用setBackgroundImage,设置button大小。

2019-3-18更新
swift版本:

    //获取侧滑手势
    func addPanGesture() {
        //setp1:需要获取系统自带滑动手势的target对象
        let target = self.navigationController?.interactivePopGestureRecognizer?.delegate
        //setp2:创建全屏滑动手势~调用系统自带滑动手势的target的action方法
        let pan = UIScreenEdgePanGestureRecognizer.init(target: target, action: Selector(("handleNavigationTransition:")))
        pan.edges = .left
        //step3:设置手势代理~拦截手势触发
        pan.delegate = self
        //step4:别忘了~给导航控制器的view添加全屏滑动手势
        self.view.addGestureRecognizer(pan)
        //step5:将系统自带的滑动手势禁用
        self.navigationController?.interactivePopGestureRecognizer?.isEnabled = false
        
    }

你可能感兴趣的:(iOS中自定义导航栏按钮的系统返回手势)