【iOS開發-22】navigationBar導覽列,navigationItem建立:擷取導覽列中的基本文本和button以及各種跳躍

來源:互聯網
上載者:User

標籤:

(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設定左右兩邊的button,一般預設的在左邊有“返回”。在右邊的有“網路攝影機”(如朋友圈)。步驟就是建立一個UIBarButtonItem對象,然後直接把這個對象賦值給self.navigationItem.leftBarButtonItem或者右邊的。當然也能夠一次建立非常多個UIBarButtonItem組成一個數組。然後把這個數組賦值給self.navigationItem.leftBarButtonItems。注意後面這個和前面這個相比,多了一個“s”。有非常多個。也要注意一下有多個button時的排列順序。


(5)我們建立的這些導航條button有非常多種形式。有的是由文字的,有的時圖片,有的時系統內建的如網路攝影機或者Reply這些icon,有的全然是自訂的視圖。

我們當然也能夠利用自己建立的導航條button來覆蓋原來導航控制器產生的預設的button,如“<Back”。


相同。須要建立兩個視圖控制器(ViewController根視圖控制器,SecondViewController子視圖控制器),然後放在導航控制器棧中。而且在AppDelegate.m中進行把導航控制器賦值給self.window.rootViewController。

在ViewController.m中:

#import "ViewController.h"#import "SecondViewController.h"@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad {    //建立一個button,點擊後進入子視圖控制器,相當於進入子頁面    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有點相似於button的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];        //設定導航條的左右button    //先執行個體化建立一個UIBarButtonItem,然後把這個button賦值給self.navigationItem.leftBarButtonItem就可以    //初始化文字的button類型有UIBarButtonItemStylePlain和UIBarButtonItemStyleDone兩種類型,差別貌似不大    UIBarButtonItem *barBtn1=[[UIBarButtonItem alloc]initWithTitle:@"左邊" style:UIBarButtonItemStylePlain target:self action:@selector(changeColor)];    self.navigationItem.leftBarButtonItem=barBtn1;        //我們還能夠在左邊和右邊加不止一個button。,且能夠加入隨意視圖,以右邊為例    //加入多個事實上就是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:@"[email protected]"] 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];    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];    [email protected]"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];        //設定導航標題,這個時候的返回button的title就是上一級的navigationItem的title文字    [self.navigationItem setTitle:@"子頁"];        //我們也能夠在子頁中自己定義一個返回button覆蓋原先的"<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

截個圖:


【iOS開發-22】navigationBar導覽列,navigationItem建立:擷取導覽列中的基本文本和button以及各種跳躍

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.