【iOS開發-23】toolbar工具條和toolbarItems:與navigationBar和navigationItem類似,重點是如何排版工具條的元素

來源:互聯網
上載者:User

標籤:style   blog   http   color   io   os   ar   for   sp   

(1)toolbar屬性、toolbarItems與上一講的navigationBar、navigationItem類似。只不過toolbarItems沒有navigationItem的左右區分,它就自己一個人在做事,相當於沒有下屬。


(2)可以在toolbar上設定很多,比如背景顏色、背景圖片、背景樣式、大小位置(不過有些貌似設定無效),當然和navigationBar一樣,對於它的是否顯示和隱藏是由它的老爸即navigationController控制的。


(3)重點是:我們可以利用toolbarItems來設定工具條上得按鈕,我們一般APP看到的下面有3-4個選項比如“首頁”、“設定”這些都可以作為UIBarButtonItem對象被載入到這個工具條上,當然,從toolbarItems名字看出來,它又個“s”,所以我們得把單獨的UIBarButtonItem對象裝到一個數組中,然後把這個數組賦值給toolbarItems。


(4)而在此過程中,我們認識到了一個很重要的UIBarButtonItem,它是系統內建的,之前我們講過照相機、reply這些系統內建的按鈕,這次說的是UIBarButtonSystemItemFlexibleSpace,嚴格說它不是一個合格的bar按鈕,而是一個可以自由縮放的地區,類似於彈簧,其他地方被佔滿了它就縮小一些,沒佔滿它就大一些。它用來排版我們這些UIBarButtonItem,有兩個UIBarButtonItem時,我們除了再首位增加一個UIBarButtonSystemItemFlexibleSpace,還在它們中間增加一個,它們就置中了,如果有三個,你猜到會怎麼做的吧。


我們就在導航控制器的根視圖控制器上做的實驗,所以此處就貼這部分代碼(當然,你要在AppDelegate.m中建立導航控制器並把我們下面的ViewController執行個體化的對象設定為根視圖控制器,並且把self.window的rootViewController變成這個導航控制器,才能正常顯示,詳見前面兩節):

#import "ViewController.h"@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad {    //設定toolbarHidden隱藏和顯示,兩種方法均可    self.navigationController.toolbarHidden=NO;    [self.navigationController setToolbarHidden:NO animated:YES];    //設定toolbarHidden背景顏色    //[self.navigationController.toolbar setBarTintColor:[UIColor redColor]];    //這個貌似沒有用(設定背景的)    //[self.navigationController.toolbar setBackgroundColor:[UIColor orangeColor]];    //設定toolbarHidden樣式,黑色,黑色透明等等,但貌似都是半透明效果    [self.navigationController.toolbar setBarStyle:UIBarStyleBlack];    //設定toolbarHidden背景圖片,forToolbarPosition是位置狀態是放在什麼地方時顯示設定它的位置,UIBarMetricsDefault是狀態設定在豎屏還是橫屏時顯示    [self.navigationController.toolbar setBackgroundImage:[UIImage imageNamed:@"navigationBar.png"] forToolbarPosition:UIBarPositionBottom barMetrics:UIBarMetricsDefault];    //可以設定位置,但貌似無效果    self.navigationController.toolbar.frame=CGRectMake(0, 0, 375, 44);        //重點是設定上面的按鈕這些    //和設定navigationBarItem類似    //先設定一個UIBarButtonItem,然後組成數組,然後把這個數組賦值給self.toolbarItems    UIBarButtonItem *btn1=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:nil];    UIBarButtonItem *btn2=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemBookmarks target:self action:nil];    UIBarButtonItem *btn3=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemCompose target:self action:nil];    UIBarButtonItem *btn4=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];    NSArray *arr1=[[NSArray alloc]initWithObjects:btn4,btn1,btn4,btn2,btn4,btn3,btn4, nil];    self.toolbarItems=arr1;        self.view.backgroundColor=[UIColor purpleColor];    [super viewDidLoad];    // Do any additional setup after loading the view, typically from a nib.}@end

截個圖:


【iOS開發-23】toolbar工具條和toolbarItems:與navigationBar和navigationItem類似,重點是如何排版工具條的元素

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.