天天看點

iOS中的UINavigationController導航欄視圖控制器的簡單應用

在這篇文章中簡單的總結了一些UINavigationController導航欄視圖控制器的簡單用法

1.首先使用這個導航欄控制器,需要将另外一個控制器設定為這個導航欄控制器的根視圖控制器

//Appdelegate.m//檔案中
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
           
[self.window  makeKeyAndVisible];
           
iOS中的UINavigationController導航欄視圖控制器的簡單應用
//RootViewController.m檔案中寫實作代碼
//導航欄控制器一般分為兩個部分 一個為UINavigationBar(導航欄) 一個為NavigationItem(導航标題欄)
           
<pre name="code" class="objc">//第一種 UINavigationBar
//設定導航欄樣式
self.navigationController.navigationBar.barStyle = UIBarStyleDefault;
           
//設定導航欄背景顔色
self.navigationController.navigationBar.backgroundColor = [UIColor yellowColor];
           
//導航欄顔色
self.navigationController.navigationBar.barTintColor = [UIColor redColor];
           
//導航欄元素顔色
self.navigationController.navigationBar.tintColor = [UIColor redColor];
           
//導航欄是否隐藏   YES為隐藏   NO不隐藏
self.navigationController.navigationBarHidden = YES;
           
//導航欄半透明效果 YES為開啟 NO為關閉
           
self.navigationController.navigitionBar.translucent = YES;
           
//第二種 navigationItem 導航标題欄
           
//導航欄标題  這兩種設定導航欄标題的方式效果是一樣的,但是它們倆有一個明顯的差別就是 第一個寫法會在修改導航欄标題的時候也會修改下面的标簽欄
           
self.title = @"我愛你";
           
self.navigationItem.title = @"你最美";
           
//更改标題視圖  比如如下
           
UISegmentedControl *seg = [[UISegmentedControl alloc] initWithItems:@[@"1", @"2"]]
           
seg.frame = CGRcetMake(0,0, 100, 100);
           
seg.selectSegmentIndex = 0;
           
self.navigationItem.titleView = seg;//替換視圖
           
//在導航欄左邊添加一個 和 多個按鍵  leftBarButtonItem   leftBarButtonItems                                                           //在導航欄右邊添加一個 和 多個按鍵  rightBarButtonItem   rightBarButtonItems<pre name="code" class="objc">//1.系統的方法
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemPlay target:self action:@selector(leftAction:)];  建立的導航欄上面左邊的按鍵
//2.使用自定義方法建立
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"icon"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(rightAction:)];  建立導航欄上面右面的按鍵
           
//頁面跳轉的方式
//第一種為通過導航欄控制器進行界面跳轉
1.[self.navigationController pushViewController:second animated:YES];  second為你将要跳轉的控制器的執行個體化對象 也叫入棧
2.[self.navigationController popViewControllerAnimated:YES];  傳回上一個界面,也叫出棧
3.[self.navigationController popToRootViewControllerAnimated:YES]; 直接跳回最開始的那一個界面
//第二種方式叫模态,直接通過控制器進行跳轉
1.[self presentViewController:second animated:YES completion:^{
        
    }]; 跳轉到這個second對象的類的界面中
2.[self dismissViewControllerAnimated:YES completion:^{
        
    }];傳回上一級界面