ios开发 向右滑动手势实现返回 -- @ShinePug

在navigationController中实现向右滑动 返回功能

系统提供的backbarbuttonitem,不用添加任何代码即可实现向右滑动后退功能,但是往往要对按钮修改样式等时,就需要自定义leftbarbuttonitem,此时向右滑动即失效.通过下面方法即可解决该问题.


主要是通过设置navigationController.interactivePopGestureRecognizer 此手势的一些属性,此手势大家可以通过sdk查看说明,这里不细说


1.  self . navigationController . interactivePopGestureRecognizer . enabled  = YES |  NO ;      手势有效与否

2. self.navigationController.interactivePopGestureRecognizer.delegate = self;               手势的代理,一般会设置为self

1中的属性,再viewcontroller中默认的设置为YES,即手势有效.按照2中的属性设置后,当前的viewcontroller即可以实现该向右滑动后退功能,但是当回到navigationController的rootView中再次做出向右滑动时,程序会有问题(再次push子controller时,程序卡在当前界面无法跳转).有效解决方案如下:

说明:有两个controllerA,B

navigationController的rootview设置为A,在A中点击按钮后push B.在A的 -(void)viewDidAppear:(BOOL)animated方法中加入self.navigationController.interactivePopGestureRecognizer.enabled = NO;代码如下:

- (void)viewDidAppear:(BOOL)animated

{

    if ([[[UIDevice currentDevicesystemVersionfloatValue] >= 7.0) {

        self.navigationController.interactivePopGestureRecognizer.enabled = NO;    //让rootView禁止滑动

    }

}

然后再B中的- (void)viewDidLoad方法中加入

- (void)viewDidLoad

{

    // 配置返回按钮

    UIBarButtonItem * backItem = [self barButtonForImageNames:@[@"icon-返回"@""@""] action:@selector(popBack)];

    backItem.title = @"";

    if ([[[UIDevice currentDevicesystemVersionfloatValue] >= 7.0) {

        self.navigationController.interactivePopGestureRecognizer.enabled = YES;

        self.navigationController.interactivePopGestureRecognizer.delegate = self;

    }

    self.navigationItem.leftBarButtonItem = backItem;


    if ([[[UIDevice currentDevicesystemVersionfloatValue] >= 7.0) {

        self.navigationController.interactivePopGestureRecognizer.delegate = self

    }

}

这样即可以保证再A中向右滑动后再次pushB时不会卡在A界面.

再项目中大家一般会创建风格统一的界面,一般都会创建一个基础viewcontroller,再此viewcontroller扩展一个配置leftbarbutton的方法,在该方法中加入B的viewDidLoad中的代码,这样在创建leftbarbutton的同时,直接加了返回的操作(不用每个viewController中都加入这样一段代码).然后只在navigationController的rootView中加入A的(void)viewDidAppear:(BOOL)animated方法即可.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值