UINavigation
来源:互联网 发布:美团数据泄露 编辑:程序博客网 时间:2024/05/13 17:26
今天又自学了一天。。哎 感觉看了一天 没看多少内容。。昨天也差不多。今天就把看的东西写上来吧,不然感觉啥事都没做。
今天在研究自定义UIBarButtonItem的时候,发现一个问题,如下。。
iOS7导航栏左侧按钮自定义影响系统返回手势问题
如题,大家知道新功能有个返回手势动作,但是当此界面自定义的导航栏的左侧按钮(右侧没事)就会出现手势实效的问题,在网上查了很久,发现大家基本都是那一两种答案!其一,设置代理,并在对应界面控制响应功能,此方法倒是可以实现返回手势,但是有Bug,主界面要是也去用返回手势操作下就会出现界面错乱,按照网上解决未果,感觉大家没有自己亲自尝试就转载了!其二,使用新特性appearance代替leftBarButtonItem设置左按钮,这样设置完的效果我是无法接受,你们可以试试,有完美的让我看看啊!目前没有找到更好的办法,你们知道吗?请指点一二!
按照楼主亲测,包括本人亲测,是可以行的通的。
目前已经解决!方案:在RootViewController的viewDidAppear中把
self.navigationController.interactivePopGestureRecognizer.enabled = NO;
并且在推出ChildViewController前调用
if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) { self.navigationController.interactivePopGestureRecognizer.enabled = YES; self.navigationController.interactivePopGestureRecognizer.delegate = nil; }
即可实现自定义与返回手势不冲突且无BUG。
但是不能在同一个xib中有多个界面,此时不会返回上级视图,而是xib前的视图。
关于BackBarButtonItem和LeftBarButtonItem
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:"返回" style:UIBarButtonItemStylePlain target:self action:nil];self.navigationItem.backBarButtonItem = backButton;
复制代码界面上backButton并没出现“返回”的字样
其实是被leftBarButtonItem和rightBarButtonItem的设置方法所迷惑了
lleftBarButtonItem和rightBarButtonItem设置的是本级页面上的BarButtonItem,而backBarButtonItem设置的是下一级页面上的BarButtonItem
比如:两个ViewController,主A和子B,我们想在A上显示“刷新”的右BarButton,B上的BackButton显示为“撤退”
就应该在A的viewDidLoad类似方法中写:
UIBarButtonItem *refreshButton = [[UIBarButtonItem alloc] initWithTitle:"刷新" style:UIBarButtonItemStylePlain target:self action:nil];self.navigationItem.rightBarButtonItem = refreshButton;UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithTitle:"撤退" style:UIBarButtonItemStylePlain target:self action:nil];self.navigationItem.backBarButtonItem = cancelButton;复制代码而B不需要做任何处理然后ApushB就可以了
官方文档UINavigationController Class Reference
使用pushViewController切换到下一个视图时,navigation controller按照以下3条顺序更改导航栏的左侧按钮。
1、如果B视图有一个自定义的左侧按钮(leftBarButtonItem),则会显示这个自定义按钮;
2、如果B没有自定义按钮,但是A视图的backBarButtonItem属性有自定义项,则显示这个自定义项;
3、如果前2条都没有,则默认显示一个后退按钮,后退按钮的标题是A视图的标题。
按照这个解释,我把UIBarButtonItem *backItem……这段代码放在A视图的pushViewController语句之前。
OK问题解决了,B视图的后退按钮的标题变成back了。
说明了backBarButtonItem只能自定义image和title,不能重写target or action,系统会忽略其他的相关设置项。
如果硬是需要重写action做一些其他的工作,则需要自定义一个leftBarButtonItem,因为系统定义leftBarButtonItem的显示优先级比backBarButtonItem优先级高,当存在leftBarButtonItem时,自动忽略backBarButtonItem,达到重写backBarButtonItem的目的。
贴上一段自己写的代码,还用到了扩展。
self.navigationItem.leftBarButtonItem = [UIBarButtonItem initWithIcon:@"返回" highlightedIcon:nil target:self action:@selector(back:)];
@interface UIBarButtonItem (Dj)+(id)initWithIcon:(NSString *)icon highlightedIcon:(NSString *)highlighted target:(id)target action:(SEL)action;@end
#import "UIBarButtonItem+Dj.h"@implementation UIBarButtonItem (Dj)+(id)initWithIcon:(NSString *)icon highlightedIcon:(NSString *)highlighted target:(id)target action:(SEL)action{ UIView *t_view = [[UIView alloc] initWithFrame:CGRectMake(0, 0,40, 44)]; t_view.backgroundColor = [UIColor clearColor]; UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom]; [leftButton setFrame:CGRectMake(0, 0,40, 44)]; [leftButton addTarget:target action:action forControlEvents:UIControlEventTouchUpInside]; [t_view addSubview:leftButton]; UILabel *t_label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 40, 44)]; [t_label setBackgroundColor:[UIColor clearColor]]; [t_label setTextAlignment:NSTextAlignmentCenter]; [t_label setTextColor:[UIColor redColor]]; [t_label setFont:[UIFont fontWithName:@"STHeitiSC-Medium" size:13]]; t_label.text = icon; [t_view addSubview:t_label]; return [[self alloc] initWithCustomView:t_view];}@end
包括 我们可以利用 [UIBarButtonItemappearance]设置全局的图像
[[UIBarButtonItem appearance] setBackButtonBackgroundImage:[UIImage imageNamed:@"play"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
- UINavigation
- UINavigation Controller
- UINavigation Controller
- UITabBar&UINavigation
- iOS UINavigation
- UINavigation详解
- UINavigation相关
- UINavigation修改
- IOS导航栏UINavigation
- UINavigation自定义返回按钮
- UINavigation Controler 切换效果
- ios UINavigation 用法示例
- UINavigation category 定制
- UINavigation 导航控制器
- iOS自定义UINavigation
- UINavigation 隐藏方式
- iOS-添加UINavigation
- UINavigation导航栏颜色
- Could not resolve view with name 'bok.html' in servlet with name 'spring'
- 示例学习kettle之Excel Writer步骤
- oracle 中的日期函数
- CopyFile(CopyFileEx)复制.mov大文件时关闭文件后文件没有复制完全
- maven项目导出war文件
- UINavigation
- 【WEB基础】④关于数据分页
- Opengl旋转-缩放
- 使用位图字体工具BMFont从图片生成自定义字体
- Android 之ActivityThead、ActivityManagerService 与activity的管理和创建
- scanf理解一
- c++ 位操作实现数据标志位获取与更新
- (转) 手机RF射频PCB板布局布线的一些经验
- Activity生命周期详解