【问题标题】:iphone - How can I use an UIBarButtonItem style without the UIToolBariphone - 如何在没有 UIToolBar 的情况下使用 UIBarButtonItem 样式
【发布时间】:2010-10-25 14:45:29
【问题描述】:

我可以在我的视图中显示一个黑色样式的 UIBarButtonItem 而不使用下面的 UIToolBar 吗?

UIToolBar总是有一种边框的东西,我希望系统UIBarButtonItem是黑色的,就像一个黑色的标准取消按钮,而不是UIToolBar

我该怎么做?

谢谢

【问题讨论】:

    标签: iphone uibarbuttonitem uitoolbar


    【解决方案1】:

    有一个厚颜无耻的 hack 可以帮助你解决这个问题。

    您想使用样式设置为UISegmentedControlStyleBar 且只有一项的 UISegmentedControl。您还需要将setMomentary 设置为是 - 使其表现得像一个按钮:

    UISegmentedControl *myCustomButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"Hello",nil]];
    [myCustomButton setSegmentedControlStyle:UISegmentedControlStyleBar];
    [myCustomButton setTintColor:[UIColor blackColor]];
    [myCustomButton setMomentary:YES];
    [self.view addSubview:myCustomButton];
    

    这将为您提供一个看起来像 UIBarButtonItem 的按钮,您可以将其添加到普通视图中,就好像它是一个按钮一样。您也可以添加目标和操作。 :)

    这就是你得到的:像这样的东西:

    【讨论】:

    • 谢谢托马斯,它成功了。但问题是如果我点击它,它不会有任何高亮功能,我的意思是一些效果来显示这个按钮被按下。如何添加高亮功能?
    • 它应该这样做......这就是setMomentary 所做的,它会像普通按钮一样暂时显示一个突出显示......尝试将 tintColor 设置为darkGrayColor 看看是否有任何区别。可能blackColor 太暗了,没有更暗的了。 :p
    【解决方案2】:

    我还没有完全做过类似的事情,但我建议你可以以某种方式将 UIToolBar 的所有子视图的 alpha 设置为零,但 UIBarButtonItem 除外。让 UIBarButtonItem 可见。

    UIToolBar 继承自 UIView,所以我的第一个尝试是将其 backgroundColor 设置为 clearColor。

    【讨论】:

      猜你喜欢
      • 2019-07-23
      • 1970-01-01
      • 2012-09-18
      • 2016-08-25
      • 1970-01-01
      • 1970-01-01
      • 2015-03-29
      • 1970-01-01
      相关资源
      最近更新 更多