導航視圖和android 中的viewpager類似
//導覽列相關的是navigationItem,控制器相關的是navigationController
0: 首先還是通過純的代碼來實現
0:刪除3個檔案ViewController.h,ViewController.m,Main.storyboard
1:修改點擊左邊的藍色按鈕,然後選擇general-》developer info-》main interface ,將這個main interface 晴空
1: 修改appdeleget
//// AppDelegate.m// NineNavigationDemo//// Created by 千雅爸爸 on 16/10/15.// Copyright © 2016年 kodulf. All rights reserved.//#import "AppDelegate.h"#import "FirstViewController.h"@interface AppDelegate ()@end@implementation AppDelegate- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds]; [self.window makeKeyAndVisible]; FirstViewController *firstViewController = [[FirstViewController alloc]init]; UINavigationController *navigationController = [[UINavigationController alloc]initWithRootViewController:firstViewController]; [self.window setRootViewController:navigationController]; return YES;}- (void)applicationWillResignActive:(UIApplication *)application { // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.}- (void)applicationDidEnterBackground:(UIApplication *)application { // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.}- (void)applicationWillEnterForeground:(UIApplication *)application { // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.}- (void)applicationDidBecomeActive:(UIApplication *)application { // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.}- (void)applicationWillTerminate:(UIApplication *)application { // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.}@end
2: 建立三個控制器,
2.1: 第一個控制器:
//// FirstViewController.m// NineNavigationDemo//// Created by 千雅爸爸 on 16/10/15.// Copyright © 2016年 kodulf. All rights reserved.//#import "FirstViewController.h"#import "SecondViewController.h"@interface FirstViewController ()@end@implementation FirstViewController- (void)viewDidLoad { //導覽列的座標是(0,20,寬度,44); //導覽列相關的是navigationItem,控制器相關的是navigationController [super viewDidLoad]; [self setTitle:@"第一頁"]; [self.view setBackgroundColor:[UIColor greenColor]];// UIBarButtonItem *item =[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:NULL]; //設定導航的item self.navigationItem.rightBarButtonItem = item; //自訂view作為導航的菜單 UIButton *itemButton = [UIButton buttonWithType:UIButtonTypeCustom]; [itemButton setTitle:@"UIButton" forState:UIControlStateNormal]; [itemButton setTitleColor:[UIColor greenColor] forState:UIControlStateHighlighted]; [itemButton setFrame:CGRectMake(0,0,100,100)]; UIBarButtonItem *item2 = [[UIBarButtonItem alloc]initWithCustomView:itemButton]; //設定多個button self.navigationItem.leftBarButtonItems = @[item,item2]; //設定導航藍上面的顏色 self.navigationController.navigationBar.backgroundColor = [UIColor blueColor]; [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"background"] forBarMetrics:UIBarMetricsCompact];//後面的是圖片的渲染的樣式的。 //UIBarMetricsDefault預設的,背景的圖片和顏色是不能重複的,像這裡圖片會覆蓋顏色的 //UIBarMetricsCompac 會使用圖片的顏色 //注意了,這裡的尺寸裡面的位置, UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; [button setTitle:@"Push" forState:UIControlStateNormal]; [button setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted]; [button setBackgroundColor:[UIColor blackColor]]; [button setFrame:CGRectMake(100,80,100,100)]; [button addTarget:self action:@selector(pushController) forControlEvents:UIControlEventTouchUpInside];//當在內部點擊的時候觸發 [self.view addSubview:button]; //導覽列控制器在控制多個視圖控制的時候的跳轉 // Do any additional setup after loading the view.}//相當於入戰的操作-(void) pushController{ NSLog(@"點擊了first"); [self.navigationController pushViewController:[[SecondViewController alloc]init] animated:YES];}- (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
2.2 第二個控制器
//// SecondViewController.m// NineNavigationDemo//// Created by 千雅爸爸 on 16/10/15.// Copyright © 2016年 kodulf. All rights reserved.//#import "SecondViewController.h"#import "FirstViewController.h"#import "ThirdViewController.h"@interface SecondViewController ()@end@implementation SecondViewController- (void)viewDidLoad { //導覽列相關的是navigationItem,控制器相關的是navigationController [super viewDidLoad]; //預設的切換的時候是有一些卡頓的。 [self.view setBackgroundColor:[UIColor yellowColor]]; [self setTitle:@"第二頁"]; //多個子控制器對象是公用一個導航條的 //添加統一的返回按鈕,簡單的就是一個子 UIBarButtonItem *leftItem =[[UIBarButtonItem alloc] initWithTitle:@"回退" style:UIBarButtonItemStylePlain target:self action:@selector(popController)]; self.navigationItem.leftBarButtonItem = leftItem; UIBarButtonItem *rightItem =[[UIBarButtonItem alloc]initWithTitle:@"下一個" style:UIBarButtonItemStylePlain target:self action:@selector(pushController)]; // Do any additional setup after loading the view. self.navigationItem.rightBarButtonItem = rightItem; //公用一套導航條的 [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];}-(void )popController{ [self.navigationController popViewControllerAnimated:YES];}-(void)pushController{ [self.navigationController pushViewController:[[ThirdViewController alloc]init] animated:YES];}- (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
2.3: 第三個控制器
//// ThirdViewController.m// NineNavigationDemo//// Created by 千雅爸爸 on 16/10/15.// Copyright © 2016年 kodulf. All rights reserved.//#import "ThirdViewController.h"@interface ThirdViewController ()@end@implementation ThirdViewController- (void)viewDidLoad { //導覽列相關的是navigationItem,控制器相關的是navigationController [super viewDidLoad]; // Do any additional setup after loading the view. //pop跳轉的時候,可以跳轉到上一個,也可以跳轉到根,也可跳轉到指定的 [self setTitle:@"第三個"]; [self.view setBackgroundColor:[UIColor whiteColor]]; UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithTitle:@"返回上一層或者最上層" style:UIBarButtonItemStylePlain target:self action:@selector(popViewController)]; //導覽列相關的是navigationItem,控制器相關的是navigationController self.navigationItem.leftBarButtonItem = leftItem; }-(void)popViewController{ //可以跳轉到第一頁,也可以跳轉到上一頁 //[self.navigationController popViewControllerAnimated:YES]; [self.navigationController popToRootViewControllerAnimated:YES];}- (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