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

3.6 按鈕項(xiàng)目

3.6.1 系統(tǒng)按鈕

可以在導(dǎo)航條、工具條中追加各種各樣的UIBarButtonItem(注意UIBarButtonItem為UIBarItem的子類,而與UIView沒有繼承關(guān)系),UIKit中事先提供了各種系統(tǒng)按鈕。創(chuàng)建系統(tǒng)按鈕時,使用UIBarButtonItem的initWithBarButtonSystemIt em:target:action:方法。以下是具體的實(shí)例代碼。

UIBarButtonItem* button =
     [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarBut tonSystemItemUndo
                  target:self
                 action:@selector(buttonDidPush)] autorelease];

上述代碼中,首先向其第一個參數(shù)中傳入常量UIBarButtonSystemItemUndo,此按鈕為刷新畫面專用的系統(tǒng)按鈕。如圖3-23所示。

圖3-23 刷新按鈕

接著向target中傳入self,向action中傳入@selector(buttonDidPush)后,當(dāng)用戶觸摸此系統(tǒng)按鈕時,將調(diào)用本類中定義的 buttonDidPush 方法。使用initWithBarButtonS ystemItem:target:action:方法,可以追加各種系統(tǒng)按鈕,當(dāng)用戶觸摸時執(zhí)行各種對應(yīng)的處理。表3-3中羅列了全部系統(tǒng)按鈕。

表3-3 系統(tǒng)按鈕列表

續(xù)表

3.6.2 工具條按鈕間距的調(diào)整

表3-3中羅列了所有的系統(tǒng)按鈕,實(shí)際UIKit中還提供了兩個沒有出現(xiàn)在表中的常量。分別是UIBarButtonSystemItemFlexibleSpace以及UIBarButtonSystemItem FixedSpace。這些也是UIBarButtonSystemItem類型常量,但是不是按鈕,而是調(diào)整按鈕間距用的對象。例如,如果沒有進(jìn)行任何處理,依次追加4個按鈕后,按鈕將顯示在工具條左側(cè),如圖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]];

這里為了讓代碼看起來更整齊,創(chuàng)建了一個新方法barButtonSystemItem:,只需要向此方法中傳入系統(tǒng)按鈕的常量就可以創(chuàng)建對應(yīng)的系統(tǒng)按鈕了,相關(guān)代碼如下。

-(UIBarButtonItem*)barButtonSystemItem:(UIBarButtonSystemItem)systemItem {
  UIBarButtonItem* button =
    [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:systemItem
                                target:nil
                                action:nil] autorelease];
  return button;
}

執(zhí)行后,將顯示如圖3-25所示的效果。

圖3-25 等間距按鈕

如上述實(shí)例所示,UIBarButtonSystemItemFlexibleSpace能自動調(diào)節(jié)按鈕間的間距。

另外,不僅可以調(diào)整按鈕間的間距,將其配置到左端(傳遞給setToolbarItems:方法的數(shù)組的第一個元素)時,可創(chuàng)建靠右的工具條按鈕(見圖3-26)。同時配置到左右端(數(shù)組的第一項(xiàng)及最后一項(xiàng))時,將創(chuàng)建居中的工具條按鈕(見圖3-27)。

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

圖3-27 居中的工具條按鈕

如果不想自動調(diào)整按鈕間的間距,而是指定固定間距值時,使用UIBarButton SystemItemFixedSpace。通過指定 UIBarButtonSystemItemFixedSpace 創(chuàng)建UIBarButtonItem實(shí)例,然后通過width屬性指定寬度。以下是實(shí)例代碼。

// 指定 UIBarButtonSystemItemFixedSpace 創(chuàng)建UIBarButtonItem實(shí)例
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]];

代碼執(zhí)行后,顯示如圖3-28所示的效果。UIBarButtonSystemItemFixedSpace 主要用于有特定按鈕顯示/隱藏間切換需要的場合,通過它當(dāng)按鈕隱藏時不至于破壞工具條的外觀。

圖3-28 固定間隔的使用效果

3.6.3 定制按鈕

上一小節(jié)介紹了UIKit中提供的系統(tǒng)按鈕,導(dǎo)航條以及工具條中還可以追加自定義按鈕,如圖3-29所示。

圖3-29 自定義按鈕

首先,可以使用initWithTitle:style:target:action:方法創(chuàng)建文本按鈕,另外還可以使用initWithImage:style:target:action:方法創(chuàng)建圖標(biāo)按鈕。圖3-30、圖3-31分別是文本按鈕以及圖標(biāo)按鈕。

圖3-30 文本按鈕

圖3-31 圖標(biāo)按鈕

指定圖標(biāo)圖片創(chuàng)建按鈕時,請注意圖片將被變換為單色。原理與標(biāo)簽條圖標(biāo)相同(請參照第3.3.3節(jié))。以下是具體的實(shí)例代碼。

// 創(chuàng)建文本按鈕
UIBarButtonItem*button = [[[UIBarButtonItem alloc] initWithTitle:@"Plain"
                     style:style
                     target:nil
                     action:nil] autorelease];
// 創(chuàng)建圖標(biāo)圖片按鈕
UIImage*image = [UIImage imageNamed:@"smile.png"];
UIBarButtonItem*icon = [[[UIBarButtonItem alloc] initWithImage:image
style:UIBarButtonItemStylePlain
                     target:self
                     action:@selector(buttonDidPush:)] autorelease];

上述兩種方法中,target與action上面已經(jīng)介紹過,與事件響應(yīng)有關(guān)。通過在style中指定不同的常量,可以改變按鈕的外觀。表3-4中羅列了常量值及其顯示的外觀區(qū)別。

表3-4 style中可指定的常量列表

其次,UIBarButtonItem中不僅可以使用文本以及圖標(biāo)圖片創(chuàng)建,還可以使用任意UIView的子類進(jìn)行創(chuàng)建(見圖3-32)。

圖3-32 定制按鈕

有不適合創(chuàng)建定制按鈕的類,下列為適合創(chuàng)建定制按鈕的代表類。

  • UIImageView。
  • UISwitch。
  • UISegmentedControl。

下面首先分別創(chuàng)建3種類的實(shí)例,然后作為參數(shù)傳遞到initWithCustomView:方法中,完成定制按鈕的創(chuàng)建。代碼如下。

// 在導(dǎo)航條中追加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]];

代碼執(zhí)行后效果如圖3-32所示,分別在導(dǎo)航條中追加了UIImageView的按鈕,在工具條中追加了UISwitch以及UISegmentedControl按鈕。

主站蜘蛛池模板: 莱阳市| 杭锦后旗| 湖北省| 通州区| 工布江达县| 普洱| 嵩明县| 昭通市| 莱西市| 扎兰屯市| 米林县| 常山县| 灵山县| 新平| 东明县| 拜城县| 小金县| 阿城市| 石阡县| 启东市| 望江县| 尼玛县| 图们市| 垫江县| 东乡族自治县| 綦江县| 桑植县| 延长县| 青河县| 丰原市| 汝城县| 信阳市| 清远市| 莱西市| 曲沃县| 兰溪市| 鹰潭市| 修水县| 高邮市| 河北省| 台南市|