工具条按钮间距的调整

原文链接:点击打开链接

表3-3中罗列了所有的系统按钮,实际UIKit中还提供了两个没有出现在表中的常量。分别是UIBarButtonSystemItemFlexibleSpace 以及  UIBarButtonSystemItem FixedSpace 。这些也是UIBarButtonSystemItem类型常量,但是不是按钮,而是调整按钮间距用的对象。例如,如果没有进行任何处理,依次追加4个按钮后,按钮将显示在工具条左侧,如图3-24所示。


 

如果要让4个按钮等间距地分布在工具条中,在使用UIViewController的setToolbarItems:方法 追加按钮时,如下述代码一样在4个按钮之间追加IBarButtonSys
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所示的效果。

如上述实例所示, UIBarButtonSystemItemFlexibleSpace 能自动调节按钮间的间距。

另外,不仅可以调整按钮间的间距,将其配置到左端(传递给setToolbarItems:方法的数组的第一个元素)时,可创建靠右的工具条按钮(见图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主要用于有特定按钮显示/隐藏间切换需要的场合,通过它当按钮隐藏时不至于破坏工具条的外观。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值