如何创建一个类似于这样的UIBarButtonItem

3
如何制作类似这样的UIBarButtonItem: enter image description here 在SystemItem值中找不到它。
谢谢
3个回答

18

你所称之为信息按钮的图像按钮,是一个系统按钮。

使用以下代码将其作为你的rightBarButtonItem获取:

UIButton* myInfoButton = [UIButton buttonWithType:UIButtonTypeInfoLight]; 
[myInfoButton addTarget:self action:@selector(InfoButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:myInfoButton];

这里是链接到UIButtonType的文档:UIButtonType


没错,但是如果你尝试这样做,你会发现你得到的是普通样式,而不是图片中显示的带边框的样式。 - André Morujão

7
那可能是使用类似以下的东西:
[[UIBarButtonItem alloc] initWithImage:@"info.png" style:UIBarButtonItemStyleBordered target:nil action:nil]

我尝试使用以下方法:

UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:[UIButton buttonWithType:UIButtonTypeInfoLight]];
item.style = UIBarButtonItemStyleBordered;

但是似乎使用自定义视图时,边框样式不会被应用。

1

以下是Swift版本的@Jhaliya的答案,可快速复制粘贴:

let infoButton = UIButton(type: .InfoLight)

infoButton.addTarget(self, action: "InfoButtonClicked:", forControlEvents: .TouchUpInside)

let infoBarButtonItem = UIBarButtonItem(customView: infoButton)

navigationItem.rightBarButtonItem = infoBarButtonItem

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