標籤:
系統按鈕
除了映像與文字按鈕,還有一個小型的系統按鈕庫,可以建立那些在許多應用程式中都可以見到的標準化的預定義按鈕。系統按鈕也是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 |
透明黑色背景、白色文字 |
iOS基礎-系統內建按鈕樣式- UIBarButtonSystemItem