1.navigationItem设置
navigationItem可以设置:
属性 | 效果 |
---|---|
title | 标题 |
titleView | 标题视图(UIView) |
leftBarButtonItem | 左边的按钮(UIBarButtonItem) |
leftBarButtonItems | 左边的按钮们(UIBarButtonItem数组) |
rightBarButtonItem | 右边的按钮 |
rightBarButtonItems | 右边的按钮们 |
backBarButonItem | 返回按钮(UIBarButtonItem) |
prompt | 描述 |
1.1 title/leftBarButtonItem/rightBarButtonItems设置
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self.view setBackgroundColor:[UIColor orangeColor]];
self.navigationController.navigationBar.translucent = NO;
CGFloat w = [UIScreen mainScreen].bounds.size.width;
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(8, 0, w-16, 220)];
imageView.image = [UIImage imageNamed:@"hy.jpg"];
[self.view addSubview:imageView];
UIBarButtonItem *item1 = [[UIBarButtonItem alloc] initWithTitle:@"item1" style:UIBarButtonItemStylePlain target:self action:@selector(btnClicked:)];
UIBarButtonItem *item2 = [[UIBarButtonItem alloc] initWithTitle:@"item2" style:UIBarButtonItemStylePlain target:self action:@selector(btnClicked:)];
self.navigationItem.title = @"Hello World";
self.navigationItem.leftBarButtonItem = item1;
self.navigationItem.rightBarButtonItems = @[item1,item2];
//leftItems/rightItems的颜色
self.navigationController.navigationBar.tintColor = [UIColor redColor];
//设置NavigationBar背景颜色
self.navigationController.navigationBar.barTintColor = [UIColor greenColor];
//设置title的文字颜色
self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName:[UIColor yellowColor]};
需要注意的是 navigationController和 navigationBar和 navigationItem三者的关系,这一点在下一小节论述.
还有,要记得如何设置title的颜色,如何设置items的颜色,如何设置navigationBar的颜色.
效果:
效果.png1.2 prompt设置
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self.view setBackgroundColor:[UIColor orangeColor]];
self.navigationController.navigationBar.translucent = NO;
CGFloat w = [UIScreen mainScreen].bounds.size.width;
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(8, 0, w-16, 220)];
imageView.image = [UIImage imageNamed:@"hy.jpg"];
[self.view addSubview:imageView];
self.navigationItem.title = @"Hello World";
//描述信息,navigationBar高度增加30,由44变成74
self.navigationItem.prompt = @"this is prompt";
}
prompt属性用来添加描述信息,赋值后,navigationBar高度增加30,由44变成74
效果
效果1.3 titleView设置
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self.view setBackgroundColor:[UIColor orangeColor]];
self.navigationController.navigationBar.translucent = NO;
CGFloat w = [UIScreen mainScreen].bounds.size.width;
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(8, 0, w-16, 220)];
imageView.image = [UIImage imageNamed:@"hy.jpg"];
[self.view addSubview:imageView];
UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 30)];
UILabel *titleLabe = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 30)];
titleLabe.text = @"newtitle";
titleLabe.textAlignment = NSTextAlignmentCenter;
titleLabe.textColor = [UIColor redColor];
[titleView addSubview:titleLabe];
titleView.backgroundColor = [UIColor yellowColor];
self.navigationItem.titleView = titleView;
}
注意
如果需要在 self.navigationItem.titleView 中添加子控件,需要先实例化一次self.navigationItem.titleView:
self.navigationItem.titleView = [[UIView alloc] init];
不然无论你忘titleView中放多少控件,都是显示不出来的.
效果
效果1.4 自定义返回按钮
-(void)btnClicked:(UIBarButtonItem *)sender{
UIViewController *vc = [[UIViewController alloc] init];
vc.view.backgroundColor = [UIColor purpleColor];
UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithTitle:@"自定义返回" style:UIBarButtonItemStylePlain target:vc action:nil];
vc.navigationItem.leftBarButtonItem = backItem;
[self.navigationController pushViewController:vc animated:YES];
}
自定义返回按钮,最简单的方法就是,设置push出了的新控制器的leftBarButtonItem,当leftBarButtonItem设置好之后, backBarButonItem就不显示了.
效果
2. navigationBar和 navigationItem的关系
(该小节参考:http://blog.csdn.net/luoyeffcs/article/details/16106707/)
先看一下这些类的继承关系,可以看清很多问题.
类 | 继承自 |
---|---|
UINavigationController | UIViewController |
UINavigationBar | UIView |
UINavigationItem | NSObject |
UIBarButtonItem | UIBarItem |
UIBarItem | NSObject |
问题1
navigationItem和navigationController都是UIViewController的属性,而UINavigationController的父类就是UIViewController,所以会出现:
self.navigationController.navigationController
和
self.navigationController.navigationItem
这种现象(self指UIViewController).而这应该是被忽略的属性(没啥乱用,容易混淆).
navigationItem是UIViewController的属性,
正确设置navigationItem:self. navigationItem
navigationBar是UINavigationController的属性,
正确设置navigationBar :self.navigationController.navigationBar
问题2
self.title 和self.navigationItem.title 和self.tabBarItem.title
由于UIViewController本身包含navigationItem和tabBarItem,所以可以单独对其title赋值,self.title会重写另外两个的值,只是提供的一种便利方法。
当self.title = @"123"时, self.navigationItem.title 的值和 self.tabBarItem.title 的值都是@"123"
问题3
UIBarItem和 UIBarButtonItem 还有 UINavigationItem的关系
UIBarItem类是一个可以放置在Bar之上的所有小控件类的抽象类。继承了该基类所有子类在外观上类似于一个Button,它们都有一个标题,图片,动作以及目标.
** UIBarButtonItem继承自UIBarItem,专门用来放在UIToolbar 或者 UINavigationBar的特殊button.基本行为跟button是一样的。
** UINavigationItem包含了当前页面导航栏上需要显示的全部信息,包括:
title, prompt, titleView, leftBarButtonItem, leftBarButtonItems, rightBarButtonItem, rightBarButtonItems, backBarButonItem
问题4
navigationBar的各种颜色设置
//设置标题
self.navigationItem.title = @"Hello World";
//设置左边的按钮
self.navigationItem.leftBarButtonItem = item1;
//设置右面的按钮 们
self.navigationItem.rightBarButtonItems = @[item1,item2];
//设置leftItems/rightItems的颜色
self.navigationController.navigationBar.tintColor = [UIColor redColor];
//设置NavigationBar背景颜色
self.navigationController.navigationBar.barTintColor = [UIColor greenColor];
//设置NavigationBar背景图片
[self.navigationController.navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
//设置title的文字颜色
self.navigationController.navigationBar.titleTextAttributes @{NSForegroundColorAttributeName:[UIColor yellowColor]};
网友评论