三个导航栏Item的区别(leftBarButtonItem/b

2019-01-23  本文已影响0人  BrianWang
1> self.navigationItem.leftBarButtonItem

2> self.navigationItem.backBarButtonItem

3> self.navigationController.navigationBar.backItem



(UINavigationItem有三种:backBarButtonItem  leftBarButtonItem  rightBarButtonItem)


> 1. 如果设置了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];
}


> 2. backBarButtonItem是返回按钮,可以自定义。注意:假如想实现第二个ViewController的backBarButtonItem,就要先在第一个ViewController中设置backBarButtonItem。

// 自定义返回按钮
UIBarButtonItem *backItem = [[UIBarButtonItem alloc] init];
[backItem setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor colorWithRed:74 / 255.0 green:74 / 255.0 blue:74 / 255.0 alpha:1.0], NSFontAttributeName:[UIFont systemFontOfSize:18]} forState:UIControlStateNormal];
backItem.title = [NSString stringWithFormat:@"返回"];
backItem.image = [UIImage imageNamed:@"NewBackButton"];
self.navigationItem.backBarButtonItem = backItem;

这样才会在第二个ViewController中有效果出现,显示返回按钮样式;​否则,则无设置效果。


> 3. backItem只是一个只读属性,不能设置。

上一篇下一篇

猜你喜欢

热点阅读