- iPhone UIKit詳解
- 王志剛 王中元 朱蕾編著
- 1658字
- 2019-01-01 07:04:28
3.6 按鈕項目
3.6.1 系統按鈕
可以在導航條、工具條中追加各種各樣的UIBarButtonItem(注意UIBarButtonItem為UIBarItem的子類,而與UIView沒有繼承關系),UIKit中事先提供了各種系統按鈕。創建系統按鈕時,使用UIBarButtonItem的initWithBarButtonSystemIt em:target:action:方法。以下是具體的實例代碼。
UIBarButtonItem* button = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarBut tonSystemItemUndo target:self action:@selector(buttonDidPush)] autorelease];
上述代碼中,首先向其第一個參數中傳入常量UIBarButtonSystemItemUndo,此按鈕為刷新畫面專用的系統按鈕。如圖3-23所示。

圖3-23 刷新按鈕
接著向target中傳入self,向action中傳入@selector(buttonDidPush)后,當用戶觸摸此系統按鈕時,將調用本類中定義的 buttonDidPush 方法。使用initWithBarButtonS ystemItem:target:action:方法,可以追加各種系統按鈕,當用戶觸摸時執行各種對應的處理。表3-3中羅列了全部系統按鈕。
表3-3 系統按鈕列表
續表
3.6.2 工具條按鈕間距的調整
表3-3中羅列了所有的系統按鈕,實際UIKit中還提供了兩個沒有出現在表中的常量。分別是UIBarButtonSystemItemFlexibleSpace以及UIBarButtonSystemItem FixedSpace。這些也是UIBarButtonSystemItem類型常量,但是不是按鈕,而是調整按鈕間距用的對象。例如,如果沒有進行任何處理,依次追加4個按鈕后,按鈕將顯示在工具條左側,如圖3-24所示。

圖3-24 左對齊的工具條按鈕
如果要讓4個按鈕等間距地分布在工具條中,在使用UIViewController的setToolbarItems:方法追加按鈕時,如下述代碼一樣在4個按鈕之間追加UIBarButtonSys temItemFlexibleSpace對象即可。
[self setToolbarItems:[NSArray arrayWithObjects: [self barButtonSystemItem:UIBarButtonSystemItemAction] // 追加間距對象UIBarButtonSystemItemFlexibleSpace [self barButtonSystemItem:UIBarButtonSystemItemFlexi bleSpace] [self barButtonSystemItem:UIBarButtonSystemItemBookmarks] // 追加間距對象UIBarButtonSystemItemFlexibleSpace [self barButtonSystemItem:UIBarButtonSystemItemFlexi bleSpace] [self barButtonSystemItem:UIBarButtonSystemItemReply] // 追加間距對象UIBarButtonSystemItemFlexibleSpace [self barButtonSystemItem:UIBarButtonSystemItemFlexi bleSpace] [self barButtonSystemItem:UIBarButtonSystemItemCompose] nil]];
這里為了讓代碼看起來更整齊,創建了一個新方法barButtonSystemItem:,只需要向此方法中傳入系統按鈕的常量就可以創建對應的系統按鈕了,相關代碼如下。
-(UIBarButtonItem*)barButtonSystemItem:(UIBarButtonSystemItem)systemItem { UIBarButtonItem* button = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:systemItem target:nil action:nil] autorelease]; return button; }
執行后,將顯示如圖3-25所示的效果。

圖3-25 等間距按鈕
如上述實例所示,UIBarButtonSystemItemFlexibleSpace能自動調節按鈕間的間距。
另外,不僅可以調整按鈕間的間距,將其配置到左端(傳遞給setToolbarItems:方法的數組的第一個元素)時,可創建靠右的工具條按鈕(見圖3-26)。同時配置到左右端(數組的第一項及最后一項)時,將創建居中的工具條按鈕(見圖3-27)。

圖3-26 靠右的工具條按鈕

圖3-27 居中的工具條按鈕
如果不想自動調整按鈕間的間距,而是指定固定間距值時,使用UIBarButton SystemItemFixedSpace。通過指定 UIBarButtonSystemItemFixedSpace 創建UIBarButtonItem實例,然后通過width屬性指定寬度。以下是實例代碼。
// 指定 UIBarButtonSystemItemFixedSpace 創建UIBarButtonItem實例 UIBarButtonItem*fixedSpace = [self barButtonSystemItem:UIBarButton SystemItemFixedSpace]; // 將寬度固定為35個像素 fixedSpace.width = 35; // 以35個像素取代其中一個按鈕 [self setToolbarItems:[NSArray arrayWithObjects: [self barButtonSystemItem:UIBarButtonSystemItemAction], [self barButtonSystemItem:UIBarButtonSystemItemFlexibleSpace], [self barButtonSystemItem:UIBarButtonSystemItemBookmarks], [self barButtonSystemItem:UIBarButtonSystemItemFlexibleSpace], fixedSpace, [self barButtonSystemItem:UIBarButtonSystemItemFlexibleSpace], [self barButtonSystemItem:UIBarButtonSystemItemCompose], nil]];
代碼執行后,顯示如圖3-28所示的效果。UIBarButtonSystemItemFixedSpace 主要用于有特定按鈕顯示/隱藏間切換需要的場合,通過它當按鈕隱藏時不至于破壞工具條的外觀。

圖3-28 固定間隔的使用效果
3.6.3 定制按鈕
上一小節介紹了UIKit中提供的系統按鈕,導航條以及工具條中還可以追加自定義按鈕,如圖3-29所示。

圖3-29 自定義按鈕
首先,可以使用initWithTitle:style:target:action:方法創建文本按鈕,另外還可以使用initWithImage:style:target:action:方法創建圖標按鈕。圖3-30、圖3-31分別是文本按鈕以及圖標按鈕。

圖3-30 文本按鈕

圖3-31 圖標按鈕
指定圖標圖片創建按鈕時,請注意圖片將被變換為單色。原理與標簽條圖標相同(請參照第3.3.3節)。以下是具體的實例代碼。
// 創建文本按鈕 UIBarButtonItem*button = [[[UIBarButtonItem alloc] initWithTitle:@"Plain" style:style target:nil action:nil] autorelease]; // 創建圖標圖片按鈕 UIImage*image = [UIImage imageNamed:@"smile.png"]; UIBarButtonItem*icon = [[[UIBarButtonItem alloc] initWithImage:image style:UIBarButtonItemStylePlain target:self action:@selector(buttonDidPush:)] autorelease];
上述兩種方法中,target與action上面已經介紹過,與事件響應有關。通過在style中指定不同的常量,可以改變按鈕的外觀。表3-4中羅列了常量值及其顯示的外觀區別。
表3-4 style中可指定的常量列表
其次,UIBarButtonItem中不僅可以使用文本以及圖標圖片創建,還可以使用任意UIView的子類進行創建(見圖3-32)。

圖3-32 定制按鈕
有不適合創建定制按鈕的類,下列為適合創建定制按鈕的代表類。
- UIImageView。
- UISwitch。
- UISegmentedControl。
下面首先分別創建3種類的實例,然后作為參數傳遞到initWithCustomView:方法中,完成定制按鈕的創建。代碼如下。
// 在導航條中追加UIImageView UIImage* image = [UIImage imageNamed:@"face.jpg"]; UIImageView*imageView = [[[UIImageView alloc] initWithImage:image] autorelease]; UIBarButtonItem* icon = [[[UIBarButtonItem alloc] initWithCustomView:imageView] autorelease]; self.navigationItem.rightBarButtonItem = icon; // 向工具條中追加UISwitch UISwitch*theSwitch = [[[UISwitch alloc] init] autorelease]; theSwitch.on = YES; UIBarButtonItem* switchBarButton = [[[UIBarButtonItem alloc] initWithCustomView:theSwitch] autorelease]; // 向工具條中追加UISegmentedControl NSArray*segments = [NSArray arrayWithObjects:@"1",@"2",@"3",nil]; UISegmentedControl* segmentedControl = [[[UISegmentedControl alloc] initWithItems:segments] autorelease]; segmentedControl.selectedSegmentIndex = 1; segmentedControl.frame = CGRectMake(0,0,100,30); UIBarButtonItem*segmentedBarButton = [[[UIBarButtonItem alloc] initWithCustomView:segmentedControl] autorelease]; [self setToolbarItems:[NSArray arrayWithObjects: switchBarButton, segmentedBarButton, nil]];
代碼執行后效果如圖3-32所示,分別在導航條中追加了UIImageView的按鈕,在工具條中追加了UISwitch以及UISegmentedControl按鈕。