http://blog.csdn.net/weisubao/article/details/39646739?utm_source=tuicool&utm_medium=referral
(1)navigationBar導航條可以看做是self.navigationController導航控制器的一個屬性,可以直接用點來表示self.navigationController.navigationBar,當然navigationBar自己還有很多屬性,比如樣式barStyle、背景backgroundColor、frame屬性(可以獲取寬高這些信息),還可以用setBackgroundImage方法設置背景圖片,當然圖片多了可以使用clipsToBounds剪裁。
(2)但,navigationBar是否隱藏和顯示這個需要它爸也就是self.navigationController來控制,有直接.navigationBarHidden設置為YES/NO,也可以用方法setNavigationBarHidden,都能實現效果。
(3)另一個重要的知識是對navigationItem的設置,這個屬性和navigationController是平級的,所以直接可以用self.navigationItem使用。當然可用的有設置導航條標題的方法setTitle,當然你也可以直接把文字換成一個視圖,即所謂的標題視圖放在導航條的中間,用得方法是setTitleView,很多游戲的導航條中間貌似是一個圖片,可以用這個。
(4)最重要的可能是給navigationItem設置左右兩邊的按鈕,一般默認的在左邊有“返回”,在右邊的有“攝像頭”(如微信朋友圈)。步驟就是創建一個UIBarButtonItem對象,然后直接把這個對象賦值給self.navigationItem.leftBarButtonItem或者右邊的。當然也可以一次創建很多個UIBarButtonItem組成一個數組,然后把這個數組賦值給self.navigationItem.leftBarButtonItems,注意后面這個和前面這個相比,多了一個“s”,有很多個。也要注意一下有多個按鈕時的排列順序。
(5)我們創建的這些導航條按鈕有很多種形式,有的是由文字的,有的時圖片,有的時系統自帶的如攝像頭或者Reply這些icon,有的完全是自己定義的視圖。我們當然也可以利用自己創建的導航條按鈕來覆蓋原來導航控制器產生的默認的按鈕,如“<Back”。
同樣,需要創建兩個視圖控制器(ViewController根視圖控制器,SecondViewController子視圖控制器),然后放在導航控制器棧中。並且在AppDelegate.m中進行把導航控制器賦值給self.window.rootViewController。
在ViewController.m中:
- #import "ViewController.h"
- #import "SecondViewController.h"
- @interface ViewController ()
- @end
- @implementation ViewController
- - (void)viewDidLoad {
- //創建一個按鈕,點擊后進入子視圖控制器,相當於進入子頁面
- UIButton *btn1=[UIButton buttonWithType:UIButtonTypeRoundedRect];
- btn1.frame=CGRectMake(38, 100, 300, 30);
- [btn1 setTitle:@"jump to secondviewcontroller" forState:UIControlStateNormal];
- btn1.backgroundColor=[UIColor whiteColor];
- self.view.backgroundColor=[UIColor redColor];
- [btn1 addTarget:self action:@selector(jumpTo) forControlEvents:UIControlEventTouchUpInside];
- [self.view addSubview:btn1];
- //設置導航條樣式
- //默認的時白色半透明(有點灰的感覺),UIBarStyleBlack,UIBarStyleBlackTranslucent,UIBarStyleBlackOpaque都是黑色半透明,其實它們有的時不透明有的時透明有的時半透明,但不知為何無效果
- self.navigationController.navigationBar.barStyle=UIBarStyleDefault;
- //設置導航條背景顏色,也是半透明玻璃狀的顏色效果
- self.navigationController.navigationBar.backgroundColor=[UIColor orangeColor];
- //可以用self.navigationController.navigationBar.frame.size獲得高寬,還有self.navigationController.navigationBar.frame.origin獲得x和y
- //高44,寬375,如果是Retina屏幕,那么寬和高@2x即可分別是750和88
- //x是0很明顯,y是20,其中上面20就是留給狀態欄的高度
- NSLog(@"%f",self.navigationController.navigationBar.frame.origin.y);
- //隱藏導航條,由此點擊進入其他視圖時導航條也會被隱藏,默認是NO
- //以下一個直接給navigationBarHidden賦值,一個調用方法,都是一樣的,下面一個多了一個動畫選項而已
- self.navigationController.navigationBarHidden=NO;
- [self.navigationController setNavigationBarHidden:NO animated:YES];
- //給導航條增加背景圖片,其中forBarMetrics有點類似於按鈕的for state狀態,即什么狀態下顯示
- //UIBarMetricsDefault-豎屏橫屏都有,橫屏導航條變寬,則自動repeat圖片
- //UIBarMetricsCompact-豎屏沒有,橫屏有,相當於之前老iOS版本里地UIBarMetricsLandscapePhone
- //UIBarMetricsCompactPrompt和UIBarMetricsDefaultPrompt暫時不知道用處,官方解釋是Applicable only in bars with the prompt property, such as UINavigationBar and UISearchBar,以后遇到時再細說
- [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"big2.png"] forBarMetrics:UIBarMetricsDefault];
- //如果圖片太大會向上擴展侵占狀態欄的位置,在狀態欄下方顯示
- //clipsToBounds就是把多余的圖片裁剪掉
- self.navigationController.navigationBar.clipsToBounds=YES;
- //設置導航標題
- [self.navigationItem setTitle:@"主頁"];
- //設置導航標題視圖,就是這一塊可以加載任意一種視圖
- //視圖的x和y無效,視圖上下左右居中顯示在標題的位置
- UIView *textView1=[[UIView alloc]initWithFrame:CGRectMake(10, 10, 50, 30)];
- textView1.backgroundColor=[UIColor whiteColor];
- [self.navigationItem setTitleView:textView1];
- //設置導航條的左右按鈕
- //先實例化創建一個UIBarButtonItem,然后把這個按鈕賦值給self.navigationItem.leftBarButtonItem即可
- //初始化文字的按鈕類型有UIBarButtonItemStylePlain和UIBarButtonItemStyleDone兩種類型,區別貌似不大
- UIBarButtonItem *barBtn1=[[UIBarButtonItem alloc]initWithTitle:@"左邊" style:UIBarButtonItemStylePlain target:self action:@selector(changeColor)];
- self.navigationItem.leftBarButtonItem=barBtn1;
- //我們還可以在左邊和右邊加不止一個按鈕,,且可以添加任意視圖,以右邊為例
- //添加多個其實就是rightBarButtonItems屬性,注意還有一個rightBarButtonItem,前者是賦予一個UIBarButtonItem對象數組,所以可以顯示多個。后者被賦值一個UIBarButtonItem對象,所以只能顯示一個
- //顯示順序,左邊:按數組順序從左向右;右邊:按數組順序從右向左
- //可以初始化成系統自帶的一些barButton,比如UIBarButtonSystemItemCamera是攝像機,還有Done,Reply等等,會顯示成一個icon圖標
- //還可以initWithImage初始化成圖片
- //還可以自定義,可以是任意一個UIView
- UIBarButtonItem *barBtn2=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:self action:@selector(changeColor2)];
- UIBarButtonItem *barBtn3=[[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"logo-40@2x.png"] style:UIBarButtonItemStylePlain target:self action:@selector(changeColor3)];
- UIView *view4=[[UIView alloc]initWithFrame:CGRectMake(10, 10, 20, 20)];
- view4.backgroundColor=[UIColor blackColor];
- UIBarButtonItem *barBtn4=[[UIBarButtonItem alloc]initWithCustomView:view4];
- NSArray *arr1=[[NSArray alloc]initWithObjects:barBtn2,barBtn3,barBtn4, nil nil];
- self.navigationItem.rightBarButtonItems=arr1;
- [super viewDidLoad];
- // Do any additional setup after loading the view, typically from a nib.
- }
- -(void)changeColor{
- self.view.backgroundColor=[UIColor purpleColor];
- }
- -(void)changeColor2{
- self.view.backgroundColor=[UIColor whiteColor];
- }
- -(void)changeColor3{
- self.view.backgroundColor=[UIColor orangeColor];
- }
- -(void)jumpTo{
- //這里面核心的有兩個,所謂跳轉,其實就是往導航控制器棧中PUSH或者POP一個視圖控制器,這樣在最上面的視圖控制器就變了,這樣視圖也跟着變了,因為只顯示在棧頂得那個視圖控制器的視圖
- //所以(1)控制所謂的跳轉,其實是導航控制器在控制,在里面的元素都可以通過navigationController屬性獲取到它們所在的導航控制器
- //所以(2)獲取到導航控制器之后,使用Push的那個方法,往棧里面放一個視圖控制器senCon1,這個新放入的在棧頂,就顯示它的視圖,所以用戶改變頁面跳轉了
- SecondViewController *senCon1=[[SecondViewController alloc]init];
- [self.navigationController pushViewController:senCon1 animated:YES];
- }
- @end
在SecondViewControllor.m中:
- #import "SecondViewController.h"
- @interface SecondViewController ()
- @end
- @implementation SecondViewController
- - (void)viewDidLoad {
- UILabel *label1=[[UILabel alloc]init];
- label1.frame=CGRectMake(38, 80, 300, 30);
- label1.backgroundColor=[UIColor whiteColor];
- label1.text=@"This is secondviewcontroller";
- [self.view addSubview:label1];
- UIButton *btn2=[UIButton buttonWithType:UIButtonTypeRoundedRect];
- btn2.frame=CGRectMake(38, 120, 300, 30);
- [btn2 setTitle:@"backTo" forState:UIControlStateNormal];
- btn2.backgroundColor=[UIColor orangeColor];
- [self.view addSubview:btn2];
- [btn2 addTarget:self action:@selector(backTo) forControlEvents:UIControlEventTouchUpInside];
- //設置導航標題,這個時候的返回按鈕的title就是上一級的navigationItem的title文字
- [self.navigationItem setTitle:@"子頁"];
- //我們也可以在子頁中自定義一個返回按鈕覆蓋原先的"<back"
- UIBarButtonItem *barBtn5=[[UIBarButtonItem alloc]initWithTitle:@"回家" style:UIBarButtonItemStylePlain target:self action:@selector(backTo)];
- self.navigationItem.leftBarButtonItem=barBtn5;
- [super viewDidLoad];
- // Do any additional setup after loading the view.
- }
- -(void)backTo{
- [self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:0] animated:YES];
- }
- @end
截個圖: