系統按鈕
除了圖像與文字按鈕,還有一個小型的系統按鈕庫,可以創建那些在許多應用程序中都可以見到的標准化的預定義按鈕。系統按鈕也是UIBarButtonItem對象,可以通過類的initWithBarButtonSystemItem方法來創建。如下例:
- UIBarButtonItem *myBookmarks = [ [ UIBarButtonItem alloc ]
- initWithBarButtonSystemItem: UIBarButtonSystemItemBookmarks
- target: self
- action: @selector(mySelector:)
- ];
表3-2是目前支持的系統按鈕,可以在UIBarButtonItem.h頭文件中找到。
表3-2
| 按鈕標識符 |
描 述 |
| UIBarButtonSystemItemDone |
藍色文字按鈕,標有“Done” |
| UIBarButtonSystemItemCancel |
文字按鈕,標有“Cancel” |
| UIBarButtonSystemItemEdit |
文字按鈕,標有“Edit” |
| UIBarButtonSystemItemSave |
藍色文字按鈕,標有“Save” |
| UIBarButtonSystemItemAdd |
圖像按鈕,上面有一個Å符號 |
| UIBarButtonSystemItemFlexibleSpace |
空白,占用空間大小可變 |
| UIBarButtonSystemItemFixedSpace |
空白占位符 |
| UIBarButtonSystemItemCompose |
圖像按鈕,上有一支筆和紙張 |
| UIBarButtonSystemItemReply |
圖像按鈕,上有一個回復箭頭 |
| UIBarButtonSystemItemAction |
圖像按鈕,上有一個動作箭頭 |
| UIBarButtonSystemItemOrganize |
圖像按鈕,上有一個文件夾以及向下箭頭 |
| UIBarButtonSystemItemBookmarks |
圖像按鈕,上有書簽圖標 |
| UIBarButtonSystemItemSearch |
圖像按鈕,上有spotlight圖標 |
| UIBarButtonSystemItemRefresh |
圖像按鈕,上有一個環形的刷新箭頭 |
| UIBarButtonSystemItemStop |
圖像按鈕,上有一個停止記號X |
| UIBarButtonSystemItemCamera |
圖像按鈕,上有一個照相機 |
| UIBarButtonSystemItemTrash |
圖像按鈕,上有一個垃圾桶 |
| UIBarButtonSystemItemPlay |
圖像按鈕,上有一個播放圖標 |
| UIBarButtonSystemItemPause |
圖像按鈕,上有一個暫停圖標 |
| UIBarButtonSystemItemRewind |
圖像按鈕,上有一個倒帶圖標 |
| UIBarButtonSystemItemFastForward |
圖像按鈕,上有一個快進圖標 |
自定義視圖按鈕
與導航欄類似,按鈕也可以按照自定義視圖類來繪制,這樣你就可以將任何一種其他類型的視圖對象作為按鈕來顯示:
- UIBarButtonItem *customButton = [ [ UIBarButtonItem alloc ]
- initWithCustomView: myView ];
創建工具欄
將所有希望顯示出來的按鈕都添加到前面創建的buttons數組:
- [ buttons addObject: buttonImage ];
- [ buttons addObject: buttonText ];
- [ buttons addObject: myBookmarks ];
下一步,創建一個UIToolbar對象,並將你的按鈕數組賦予工具欄作為項目列表:
- UIToolbar *toolbar = [ [ UIToolbar alloc ] init ];
- [ toolbar setItems: buttons animated: YES ];
最后,將你的導航欄的標題視圖替換為新創建的工具欄,就像替換成分段控件一樣:
- self.navigationItem.titleView = toolbar;
當導航欄顯示出來時,工具欄就會出現在它的中央。
調整大小
工具欄會對加入的按鈕套用默認大小。如果你希望調整工具欄,讓它可以更干凈利落地適應導航欄的大小,可以用sizeToFit方法:
- [ toolbar sizeToFit ];
工具欄的風格
就像許多其他基於視圖的對象一樣,UIToolbar也包含有一個風格屬性,名為barStyle。這個屬性可以用來調整工具欄的風格,令其與你為導航欄定義的風格相匹配:
- toolbar.barStyle = UIBarStyleDefault;
可以將工具欄的風格設置為三種標准風格主題之一,這些主題也為大多數其他類型的欄狀對象所使用,如表3-3所示。
表3-3
| 風 格 |
描 述 |
| UIBarStyleDefault |
默認風格;灰色背景、白色文字 |
| UIBarStyleBlackOpaque |
純黑色背景、白色文字 |
| UIBarStyleBlackTranslucent |
透明黑色背景、白色文字 |
