iOS 导航栏自带页面右滑pop使用示例
程序员文章站
2023-12-17 15:48:46
ios7之后苹果导航控制器增加了一个左侧边缘右滑pop的手势,在非栈底的控制器里面,从左边右滑之后相当于出栈(pop)样子。
以下设置均在自定义的uinavigation...
ios7之后苹果导航控制器增加了一个左侧边缘右滑pop的手势,在非栈底的控制器里面,从左边右滑之后相当于出栈(pop)样子。
以下设置均在自定义的uinavigationcontroller里操作
// 默认是yes 设置为no左侧右滑手势就不可用了 self.interactivepopgesturerecognizer.enabled = yes;
也可以通过手势实现右滑
遵守uigesturerecognizerdelegate协议,设置代理对象,实现一下代理方法即可:
-(void)viewdidappear:(bool)animated { [super viewdidappear:animated]; self.navigationcontroller.interactivepopgesturerecognizer.delegate = self; } #pragma mark - uigesturerecognizerdelegate - (bool)gesturerecognizershouldbegin:(uigesturerecognizer *)gesturerecognizer { if (self.viewcontrollers.count <= 1) { return no; } return yes; }
个人的记忆方法:
backbarbuttonitem设置下一个页面的返回按钮
leftbarbuttonitem设置当前页面的返回按钮
项目中我们多数会自定义左侧返回按钮leftbarbuttonitem,这个时候就算设置self.interactivepopgesturerecognizer.enabled = yes了或者实现手势的代理,左边缘的右滑动手势依旧会失效,例如如下代码写过之后,可以验证一下,失效了。
- (void)pushviewcontroller:(uiviewcontroller *)viewcontroller animated:(bool)animated { if (self.viewcontrollers.count > 0) { /* 不是栈底控制器的时候隐藏tabbar */ viewcontroller.hidesbottombarwhenpushed = yes; // 设置左边的返回按钮leftbarbuttonitem uibutton *backbtn = [uibutton buttonwithtype:uibuttontypecustom]; backbtn.frame = cgrectmake(0, 0, 44, 44); backbtn.imageview.contentmode = uiviewcontentmodecenter; backbtn.contentedgeinsets = uiedgeinsetsmake(0, 0, 0, 34); [backbtn setimage:[uiimage imagenamed:@"rrrr"] forstate:uicontrolstatenormal]; [backbtn setimage:[uiimage imagenamed:@"rrrr"] forstate:uicontrolstatehighlighted]; [backbtn addtarget: self action: @selector(back) forcontrolevents: uicontroleventtouchupinside]; uibarbuttonitem *backbtnitem=[[uibarbuttonitem alloc]initwithcustomview:backbtn]; viewcontroller.navigationitem.leftbarbuttonitem = backbtnitem; // 此处改为设置backbarbuttonitem 右滑手势可以用,但自定义按钮不展示!! } [super pushviewcontroller:viewcontroller animated:animated]; }
按照如下方式设置backbarbuttonitem或leftbarbuttonitem,其他不做任何设置,右滑效果可用:
- (void)pushviewcontroller:(uiviewcontroller *)viewcontroller animated:(bool)animated { if (self.viewcontrollers.count > 0) { /* 不是栈底控制器的时候隐藏tabbar */ viewcontroller.hidesbottombarwhenpushed = yes; } [self.navigationbar setbackindicatorimage:[uiimage imagenamed:@"rrrr"]]; [self.navigationbar setbackindicatortransitionmaskimage:[uiimage imagenamed:@"rrrr"]]; uibarbuttonitem *backitem = [[uibarbuttonitem alloc] initwithtitle:@"" style:uibarbuttonitemstyleplain target:nil action:nil]; self.navigationitem.backbarbuttonitem = backitem; [super pushviewcontroller:viewcontroller animated:animated]; }
或者
- (void)pushviewcontroller:(uiviewcontroller *)viewcontroller animated:(bool)animated { if (self.viewcontrollers.count > 0) { /* 不是栈底控制器的时候隐藏tabbar */ viewcontroller.hidesbottombarwhenpushed = yes; [self.navigationbar setbackindicatorimage:[uiimage imagenamed:@"rrrr"]]; [self.navigationbar setbackindicatortransitionmaskimage:[uiimage imagenamed:@"rrrr"]]; uibarbuttonitem *backitem = [[uibarbuttonitem alloc] initwithtitle:@"" style:uibarbuttonitemstyleplain target:nil action:nil]; self.navigationitem.leftbarbuttonitem = backitem; } [super pushviewcontroller:viewcontroller animated:animated]; }
进过上面的反复测试:在自定义导航栏控制器中backbarbuttonitem不能用view视图方法自定义,而leftbarbuttonitem可以。
setbackindicatorimage和setbackindicatortransitionmaskimage无论怎么设置都是只能看到setbackindicatorimage的图片,找不到原因!!!
然后尝试直接在root视图控制器里面设置返回按钮backbarbuttonitem的自定义view,右滑手势可用,但是设置leftbarbuttonitem就不能使用右滑手势:
- (void)viewdidload { [super viewdidload]; // 设置左边的返回按钮 uibutton *backbtn = [uibutton buttonwithtype:uibuttontypecustom]; backbtn.frame = cgrectmake(0, 0, 44, 44); backbtn.imageview.contentmode = uiviewcontentmodecenter; backbtn.contentedgeinsets = uiedgeinsetsmake(0, 0, 0, 34); [backbtn setimage:[uiimage imagenamed:@"rrrr"] forstate:uicontrolstatenormal]; [backbtn setimage:[uiimage imagenamed:@"rrrr"] forstate:uicontrolstatehighlighted]; [backbtn addtarget: self action: @selector(back) forcontrolevents: uicontroleventtouchupinside]; backbtn.backgroundcolor = [uicolor redcolor]; [self.navigationcontroller.navigationbar setbackindicatorimage:[uiimage imagenamed:@"rrrr"]]; [self.navigationcontroller.navigationbar setbackindicatortransitionmaskimage:[uiimage imagenamed:@"rrrr"]]; uibarbuttonitem *backbtnitem=[[uibarbuttonitem alloc]initwithcustomview:backbtn]; backbtnitem.title = @""; self.navigationitem.backbarbuttonitem = backbtnitem; }
以上是自己的验证,希望对大家的学习有所帮助,也希望大家多多支持。
推荐阅读