- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor whiteColor];
//设置标题文字
self.title = @"页面一";
self.navigationItem.title = @"页面一";
//back 为默认标题文字,默认文字不显示
//navigationBar是navigationController共用的,navigationItem不是共用的,需每个页面单独设置
//找到navigationItem:self.navigationItem
//设置自定义标题头
//设置自定义标题头时:位置无效,默认居中显示
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(800, 0, 200, 40)];
label.backgroundColor = [UIColor redColor];
label.text = @"页面";
label.textAlignment = NSTextAlignmentCenter;
label.textColor = [UIColor cyanColor];
label.font = [UIFont systemFontOfSize:24.0];
self.navigationItem.titleView = label;
//设置左按钮、右按钮
//文字类型
// self.navigationItem.leftBarButtonItem 左按钮
// self.navigationItem.rightBarButtonItem 右按钮
//实例化 UIBarButtonItem
UIBarButtonItem *leftBar = [[UIBarButtonItem alloc]initWithTitle:@"left" style:UIBarButtonItemStylePlain target:self action:@selector(buttonDown:)];
//将实例化的UIBarButtonItem赋给leftBarButtonItem
self.navigationItem.leftBarButtonItem = leftBar;
//图片类型
UIBarButtonItem *imageBar = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"itemImage.png"] style:UIBarButtonItemStylePlain target:self action:@selector(buttonDown:)];
//将实例化的UIBarButtonItem赋给rightBarButtonItem
self.navigationItem.rightBarButtonItem = imageBar;
//系统图片类
/*
1、UIBarButtonSystemItemAction 分享
2、UIBarButtonSystemItemAdd +
3、UIBarButtonSystemItemBookmarks 书
4、UIBarButtonSystemItemCamera 照相机
.
.
.
*/
UIBarButtonItem *systemImage = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(buttonDown:)];
self.navigationItem.leftBarButtonItem = systemImage;
//自定义按钮
UIButton *customButton = [UIButton buttonWithType:UIButtonTypeSystem];
customButton.frame = CGRectMake(0, 0, 40, 40);
customButton.backgroundColor = [UIColor orangeColor];
[customButton setTitle:@"btn" forState:UIControlStateNormal];
[customButton addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *custom = [[UIBarButtonItem alloc]initWithCustomView:customButton];
self.navigationItem.leftBarButtonItem = custom;
//多个按钮
//数组里的元素为UIBarButtonItem实例化出来的对象
//放置多个按钮时:左右按钮不能同时用一个相同的数组
// //左按钮时:按数组顺序由左向右展示
// NSArray *arr = @[systemImage,custom];
// self.navigationItem.leftBarButtonItems = @[systemImage,custom];
// //右按钮时:按数组顺序由右向左展示
// self.navigationItem.rightBarButtonItems = @[leftBar,imageBar];
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
button.frame = CGRectMake(20, 100, 280, 40);
button.backgroundColor = [UIColor redColor];
[button setTitle:@"push" forState:UIControlStateNormal];
[button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
//UIBarButtonItem方法带参数的写法
- (void)buttonDown:(UIBarButtonItem *)button{
NSLog(@"UIBarButtonItem");
}
- (void)buttonClick:(UIButton *)button{
SecondViewController *second = [[SecondViewController alloc]init];
[self.navigationController pushViewController:second animated:YES];
}