// // AppDelegate.m // UI3_UIbarButtonItem // // Created by zhangxueming on 15/7/6. // Copyright (c) 2015年 zhangxueming. All rights reserved. // #import "AppDelegate.h" @interface AppDelegate () @end @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:self.window.rootViewController]; self.window.rootViewController = nav; return YES; }
// // ViewController.m // UI3_UIbarButtonItem // // Created by zhangxueming on 15/7/6. // Copyright (c) 2015年 zhangxueming. All rights reserved. // #import "ViewController.h" #import "SecondViewController.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. self.view.backgroundColor = [UIColor cyanColor]; //每一个ViewController都对应一个NavigationItem对象 //设置标题 self.navigationItem.title = @"导航控制器"; //设置 self.title 默认把title的值赋值给navigationItem.title self.title = @"导航导航"; NSLog(@"%@", self.navigationItem.title); UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem]; btn.frame = CGRectMake(100, 200, self.view.frame.size.width-200, 50); [btn setTitle:@"点击" forState:UIControlStateNormal]; btn.titleLabel.font = [UIFont boldSystemFontOfSize:24]; [btn addTarget:self action:@selector(btnClicked) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:btn]; } - (void)btnClicked { SecondViewController *svc =[[SecondViewController alloc] init]; [self.navigationController pushViewController:svc animated:YES]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
// // SecondViewController.m // UI3_UIbarButtonItem // // Created by zhangxueming on 15/7/6. // Copyright (c) 2015年 zhangxueming. All rights reserved. // #import "SecondViewController.h" @interface SecondViewController () @end @implementation SecondViewController - (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; NSLog(@"bar = %@", self.navigationController.navigationBar); } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. self.view.backgroundColor = [UIColor yellowColor]; self.navigationItem.title = @"SecondView"; //设置提白后, navigationBar 的高度变为74 self.navigationItem.prompt = @"提白"; //设置标题视图 UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"musicTitle"]]; //设置标题视图 orgin坐标无效 imageView.frame = CGRectMake(0, 0, 20, 30); self.navigationItem.titleView = imageView; NSLog(@"imageView = %@", self.navigationItem.titleView); self.navigationItem.backBarButtonItem.title = @"返回"; NSLog(@"title = %@", self.navigationItem.backBarButtonItem.title); //初始化标题 UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(backButtonClicked)]; self.navigationItem.leftBarButtonItem = backButton; //使用系统提供的样式 UIBarButtonItem *systemBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:self action:@selector(systemBtnClicked)]; self.navigationItem.rightBarButtonItem = systemBtn; //自定义UIBarButtonItem UIButton *customBtn = [UIButton buttonWithType:UIButtonTypeCustom]; [customBtn setBackgroundImage:[UIImage imageNamed:@"customImage@2x"] forState:UIControlStateNormal]; customBtn.frame = CGRectMake(0, 0, 22, 22); [customBtn addTarget:self action:@selector(customBtnClicked) forControlEvents:UIControlEventTouchUpInside]; UIBarButtonItem *customBtnItem = [[UIBarButtonItem alloc] initWithCustomView:customBtn]; NSArray *items = [NSArray arrayWithObjects:customBtnItem,systemBtn, nil]; self.navigationItem.rightBarButtonItems = items; UIBarButtonItem *editBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:@selector(editBtnClicked)]; NSArray *leftItems = [NSArray arrayWithObjects:backButton,editBtn, nil]; self.navigationItem.leftBarButtonItems = leftItems; } - (void)editBtnClicked { NSLog(@"编辑按钮被点击"); } - (void)backButtonClicked { [self.navigationController popViewControllerAnimated:YES]; } - (void)systemBtnClicked { NSLog(@"相机功能被点击"); } - (void)customBtnClicked { NSLog(@"自定义按钮被点击"); } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } /* #pragma mark - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { // Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller. } */ @end