APPDelegate:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
//创建主界面,导航栏的第一个页面
FirstViewController *fvc = [[FirstViewController alloc]init];
//创建一个导航栏控制器
UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:fvc];
nav.navigationBar.barTintColor = [UIColor redColor];
self.window.rootViewController = nav;//设置根控制器
[self.window makeKeyAndVisible];
return YES;
}
//给导航栏添加左右按钮
UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc]initWithTitle:@"cancel" style:UIBarButtonItemStyleDone target:nil action:nil];
self.navigationItem.leftBarButtonItem = cancelButton;
self.navigationItem.leftBarButtonItem.tintColor = [UIColor whiteColor];
UIBarButtonItem *nextButton = [[UIBarButtonItem alloc]initWithTitle:@"next" style:UIBarButtonItemStyleDone target:self action:@selector(goToSecond)];
self.navigationItem.rightBarButtonItem = nextButton ;
self.navigationItem.rightBarButtonItem.tintColor = [UIColor whiteColor];
UILabel *yLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 44)];
yLabel.backgroundColor = [UIColor yellowColor];
yLabel.text = @"新浪";
yLabel.font = [UIFont systemFontOfSize:22];
yLabel.textColor = [UIColor redColor];
yLabel.textAlignment = NSTextAlignmentCenter;
self.navigationItem.titleView = yLabel;
-(void)goToSecond{
//创建即将显示的界面
SecondViewController *svc = [[SecondViewController alloc]init];
//使用导航栏控制器切换页面
// [self.navigationController presentViewController:svc animated:YES completion:nil];
//push入栈 将当前的界面入栈,这个界面消失,将新的界面推送出来
//pop 将当前的界面消失,从栈上最上面的界面(最后一个界面)出栈
//设置默认提供的返回按钮的标题
UIBarButtonItem *backItem = [[UIBarButtonItem alloc]initWithTitle:@"返回" style:UIBarButtonItemStyleDone target:nil action:nil];
self.navigationItem.backBarButtonItem = backItem;
[self.navigationController showViewController:svc sender:self];
}
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
//显示工具栏
self.navigationController.toolbarHidden = NO;
self.navigationController.toolbar.barTintColor = [UIColor greenColor];
//默认工具栏高44,导航栏高44
//可以隐藏导航栏
// self.navigationController.navigationBarHidden = YES;
}
SecondViewController类.m
-(void)viewDidLoad{
[super viewDidLoad];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btn setTitle:@"back" forState:UIControlStateNormal];
btn.frame = CGRectMake(100, 200, 120, 50);
[btn addTarget:self action:@selector(goback) forControlEvents:UIControlEventTouchUpInside];
btn.tintColor = [UIColor whiteColor];
[self.view addSubview:btn];
}
-(void)goback{
//present 在现有的界面上盖上一层,dismissViewController删除
//如果之前使用 的push 那么用pop
[self.navigationController popViewControllerAnimated:YES];
[self dismissViewControllerAnimated:YES completion:nil];
}
拓:navigationbar 上加多个button:
UIView *rightBarView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 60, 31)];UIButton *phonebutton = [UIButton buttonWithType:UIButtonTypeCustom];phonebutton.frame=CGRectMake(0, 5, 25, 25);[phonebutton setImage:[UIImage imageNamed:@"phone.png"] forState:UIControlStateNormal];[phonebutton addTarget:self action:@selector(phoneclick)forControlEvents:UIControlEventTouchDown];[rightBarView addSubview:phonebutton];UIButton *mapbutton = [UIButton buttonWithType:UIButtonTypeCustom];[mapbutton setFrame:CGRectMake(30, 5, 25, 25)];[mapbutton setImage:[UIImage imageNamed:@"c_address.png"] forState:UIControlStateNormal];[mapbutton addTarget:self action:@selector(mapclick)forControlEvents:UIControlEventTouchDown];[rightBarView addSubview:phonebutton];[rightBarView addSubview:mapbutton];rightBarView.backgroundColor=[UIColor clearColor];UIBarButtonItem *rightBtn = [[UIBarButtonItem alloc]initWithCustomView:rightBarView];self.navigationItem.rightBarButtonItem = rightBtn;
更简便的方法: