iPhone - 如何在没有 UIToolBar 的情况下使用 UIBarButtonItem 样式

3

我能在没有UIToolBar的情况下展示黑色样式的UIBarButtonItem吗?

UIToolBar总是有一种边框的东西,我想让系统的UIBarButtonItem呈现为黑色,就像一个标准的黑色取消按钮,而不是UIToolBar。

我该怎么做?

谢谢

2个回答

3

有一个聪明的技巧可以帮助您解决此问题。

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

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

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

您将得到以下内容:类似于此:

alt text


谢谢Thomas,它起作用了。但问题是,如果我点击它,它不会有任何突出特征,我的意思是一些效果来显示这个按钮被按下了。我该如何添加一个突出特征? - Jack
它应该做...这就是setMomentary的作用,它会短暂地显示一个高亮,就像普通按钮一样...尝试将tintColor设置为darkGrayColor,看看是否有任何区别。也许blackColor太暗了,没有更暗的了。 :p - Thomas Clayson

0

我并没有做过类似的事情,但我建议你可以以某种方式将所有UIToolBar的子视图alpha设置为零,除了UIBarButtonItem。只需保留UIBarButtonItem可见。

UIToolBar继承自UIView,因此我的第一次尝试是将其backgroundColor设置为clearColor。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接