【问题标题】:Change a UIBarButtonItem's style to a BarButtonSystemItem将 UIBarButtonItem 的样式更改为 BarButtonSystemItem
【发布时间】:2012-02-03 21:38:22
【问题描述】:

我是 iPhone 开发的新手,所以我不确定这是否是一个非常常见的问题。我在 Google 上没有找到任何相关信息。

我有一个使用 Interface Builder 定义的 UIBarButtonItem:

@property (weak, nonatomic) IBOutlet UIBarButtonItem *cameraButton;

目前,它只是一个简单的按钮,但我想在上面放一张相机图像,使用 UIBarButtonSystemItemCamera。我知道使用initWithBarButtonSystemItem 可以做到这一点,但我没有像changeWithBarButtonSystemItem 这样的方法。我想要类似的东西:

- (void)viewDidLoad
{
  [super viewDidLoad];

  [cameraButton changeWithBarButtonSystemItem:UIBarButtonSystemItemCamera];
}

这怎么可能?有没有办法告诉 Interface Builder 直接实例化带有这个相机图标的按钮?


编辑

似乎无法在实例化后更改按钮样式。所以只剩下一个问题:有没有办法告诉 Interface Builder 用 UIBarButtonSystemItemCamera 实例化一个按钮?

【问题讨论】:

  • “这个问题”链接似乎是指本文档。这是故意的吗?
  • 这是一个糟糕的复制粘贴。感谢您强调它。
  • 这里是解决方案。 stackoverflow.com/a/33341176/2032045

标签: iphone ios4 interface-builder uibarbuttonitemstyle


【解决方案1】:

在 Interface Builder 中,将 UIBarButtonItem 的“标识符”更改为“相机”。

如果您需要在运行时更改按钮,最简单的方法是自行更换按钮,而不是尝试改变它们。

【讨论】:

  • 谢谢,它成功了(当然)!稍微精确一点:“换出”是指我必须删除它并添加另一个按钮,因为它是不可变的?
  • 是的。如果需要,您可以轻松地在一个类别中编写一个 -replaceButton:withButton: 方法,例如 UIToolbar 来干净地封装此行为。
  • 这是一个合适的解决方案。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-07
  • 1970-01-01
  • 2023-03-12
  • 1970-01-01
  • 1970-01-01
  • 2020-07-09
相关资源
最近更新 更多