官术网_书友最值得收藏!

3.5 工具條

3.5.1 工具條的顯示

從iPhone OS 3.0開始,通過調用UIViewController的setToolbarItems:animated:方法可以簡單地在畫面中追加工具條。下面是簡單的實例代碼。

// 工具條左側顯示的按鈕
UIBarButtonItem* button1 =
  [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButt onSystemItemRefresh
                   target:self
                   action:@selector(buttonDidPush)] autorelease];
// 自動伸縮按鈕以及按鈕間的空白
UIBarButtonItem*spacer =
   [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarBut tonSystemItemFlexibleSpace
                    target:nil
                    action:nil ] autorelease];
// 工具條右側顯示的按鈕
UIBarButtonItem* button2 =
   [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarBut tonSystemItemUndo
                   target:self
                   action:@selector(buttonDidPush)] autorelease];
// 全部保存到NSArray中
NSArray*buttons = [NSArray arrayWithObjects:button1,spacer,button2,nil];
// 將準備好的NSArray作為工具條的項目設置進去
[self setToolbarItems:buttons animated:YES];

首先,如果要顯示工具條,必須將UINavigationController的toolbarHidden屬性設置為NO(也可調用setToolBarHidden:animated:方法進行設置)。但是這樣只會顯示一個空的工具條,必須通過調用UIViewController的setToolbarItems:animated:方法在工具條中設置各種按鈕項目(UIBarButtonItem)。實例代碼執行后,會顯示如圖3-19所示的畫面,在畫面的下方顯示設置的工具條。

圖3-19 工具條實例

3.5.2 工具條的自動隱藏

上一小節介紹了工具條的顯示方法。但是,一旦工具條顯示,只要沒有明確隱藏它,跳轉到下一畫面后,工具條仍然會保持顯示狀態,如圖3-20所示。

圖3-20 畫面跳轉后工具條仍然顯示

有的時候并不希望工具條一直顯示著。此時就要用到UIViewController的hidesBottomBarWhenPushed屬性。在UINavigationController中調用pushViewController方法跳轉到下一個畫面前,將此畫面的hidesBottomBarWhenPushed屬性設置為YES后,以后的畫面將隱藏工具條。設置toolbarHidden屬性或者調用setToolbar Hidden:animated:方法也能實現隱藏工具條,但是使用hidesBottomBarWhenPushed屬性后,當再次返回到原畫面時工具條將自動顯示,而上述兩種方式將一直隱藏工具條。實現效果如圖3-21所示。

圖3-21 使用hidesBottomBarWhenPushed屬性后的效果

注意:hidesBottomBarWhenPushed屬性僅僅是在畫面跳轉的時候決定是否隱藏工具條。畫面跳轉后,如果將UINavigationController的toolbarHidden屬性設置成了NO,程序還會以后者的設置優先。

3.5.3 向工具條中追加按鈕

關于向工具條中追加按鈕的知識,請參照下面第3.6節的介紹。

3.5.4 工具條的顏色

可以通過改變UIToolbar類的tintColor屬性,改變導航條的背景顏色。以下是實例代碼。

// 將工具條變成藍色
self.navigationController.toolbar.tintColor = [UIColor blueColor];

此代碼的執行結果如圖3-22所示(圖中工具條的背景實際為藍色)。

圖3-22 改變工具條的顏色

主站蜘蛛池模板: 绥芬河市| 清新县| 徐汇区| 出国| 革吉县| 湘阴县| 涿鹿县| 武夷山市| 西青区| 桂东县| 铜川市| 玉田县| 梓潼县| 乳源| 洪雅县| 汾西县| 宿迁市| 金寨县| 甘泉县| 临漳县| 安阳县| 赤壁市| 扶沟县| 博罗县| 潜江市| 湘乡市| 南陵县| 乐平市| 唐海县| 莫力| 泾阳县| 九龙坡区| 五华县| 天祝| 子洲县| 梓潼县| 太湖县| 军事| 宜宾市| 临猗县| 白玉县|