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;
?更簡便的方法: