三个导航栏Item的区别(leftBarButtonItem/backBarButtonItem/backItem)
三种导航栏Item为:
1、self.navigationItem.leftBarButtonItem
2、self.navigationItem.backBarButtonItem
3、self.navigationController.navigationBar.backItem
其中UINavigationItem
有三种:backBarButtonItem
、leftBarButtonItem
、rightBarButtonItem
。
一、leftBarButtonItem
如果设置了leftBarButtonItem
,会替换掉backBarButtonItem
。但可以设置leftItemsSupplementBackButton
为YES
,来同时显示leftBarButtonItem
和backBarButtonItem
。
leftBarButtonItem
的自定义方式如下:
UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[backBtn setTitleColor:[UIColor colorWithRed:74 / 255.0 green:74 / 255.0 blue:74 / 255.0 alpha:1.0] forState:UIControlStateNormal];
[backBtn setTitle:@"返回" forState:UIControlStateNormal];
[backBtn setImage:[UIImage imageNamed:@"NewBackButton"] forState:UIControlStateNormal];
[backBtn addTarget:self action:@selector(popViewController:) forControlEvents:UIControlEventTouchUpInside];
[backBtn sizeToFit];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backBtn];
- (void)popViewController:(UIButton *)sender {
[self.navigationController popViewControllerAnimated:YES];
}
二、backBarButtonItem
backBarButtonItem
是返回按钮,可以自定义。注意:假如想实现第二个ViewController
的backBarButtonItem
,就要先在第一个ViewController
中设置backBarButtonItem
。返回按钮
backBarButtonItem
的自定义方式如下:
UIBarButtonItem *backBarButtonItem = [[UIBarButtonItem alloc] init];
[backBarButtonItem setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor colorWithRed:74 / 255.0 green:74 / 255.0 blue:74 / 255.0 alpha:1.0], NSFontAttributeName:[UIFont systemFontOfSize:18]} forState:UIControlStateNormal];
backBarButtonItem.title = [NSString stringWithFormat:@"返回"];
backBarButtonItem.image = [UIImage imageNamed:@"NewBackButton"];
self.navigationItem.backBarButtonItem = backBarButtonItem;
这样才会在第二个
ViewController
中有效果出现,显示返回按钮样式;?否则,无设置效果。【三个导航栏Item的区别(leftBarButtonItem/backBarButtonItem/backItem)】三、backItem
backItem
只是一个只读属性,不能设置。推荐阅读
- 放屁有这三个特征的,请注意啦!这说明你的身体毒素太多
- CET4听力微技能一
- IDEA|IDEA 创建工程
- Android超简单实现沉浸式状态栏
- www和https://又被Chrome地址栏隐藏了
- 普通人进阶的三个过程-很多人都知道,但却本末倒置
- 2019-11-24|2019-11-24 第三个复盘日
- Python专栏|数据分析的常规流程
- 内容平台做电商的三个优势
- 复盘学习《通往财富自由之路》专栏——“我们重新理解了付费”