iOS 无法正确更改 UIBarButtonItem 图像
当我尝试更改 UINavigationBar 的“后退”按钮自定义图标时遇到问题。为了实现这样的目标,我使用以下代码:
- (void)viewDidLoad {
[super viewDidLoad];
UIBarButtonItem* leftBtn;
UIImage* botonVolverImg = [UIImage imageNamed:@"boton_volver.png"];
leftBtn = self.navigationItem.leftBarButtonItem;
leftBtn.image = botonVolverImg;
}
但是,当视图出现时,您会看到以下内容:
一只忙碌的猫http://www.timotteo.com.ar/boton.png
(你可以看到旧的按钮仍然出现在后面,加上我选择的图像看起来有点拉伸)
我一直在改变imageInsets 属性,但这似乎不起作用。我也一直在阅读周围的论坛,但找不到确切的解决方案。
欢迎任何建议!
I'm having a problem when I try to change a UINavigationBar's "back" button custom icon. To achieve such a thing, I'm using the following code:
- (void)viewDidLoad {
[super viewDidLoad];
UIBarButtonItem* leftBtn;
UIImage* botonVolverImg = [UIImage imageNamed:@"boton_volver.png"];
leftBtn = self.navigationItem.leftBarButtonItem;
leftBtn.image = botonVolverImg;
}
But, when the view appears, you see this:
a busy cat http://www.timotteo.com.ar/boton.png
(You can see that the old button still appears at the back, plus the image I chose looks a bit streched)
I've been changing the imageInsets property, but that doesn't seem to work. I've also been reading the forum around, but couldn´t find the exact solution.
Any suggestions are welcomed!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
UIBarButtonItem
的image
属性不对应于背景图像,仅对应于提供附加上下文的图像。如果您的目标是 5.0+,最好的选择是使用-setBackButtonBackgroundImage:forState:barMetrics:
为栏按钮项目设置背景。The
image
property forUIBarButtonItem
doesn't correspond to the background image, only an image that provides additional context. If you're targeting 5.0+, your best bet would be to use-setBackButtonBackgroundImage:forState:barMetrics:
to set a background for the bar button item.为了补充 Mark 的答案,如果您的客户要求您支持 iOS 4,您可以创建一个
UIButton
,它看起来完全符合客户的要求(即,只是该图像),然后使用UIBarButton
的initWithCustomView
方法来创建您自己的后退按钮。然后,您可以让该按钮触发popViewController
或您需要的任何适当的操作。To supplement Mark's answer, if your customer is requiring you to support iOS 4, you could create a
UIButton
that looks exactly how your customer wants it to (ie, just that image), and then useUIBarButton
'sinitWithCustomView
method to create your own back button. You can then have that button triggerpopViewController
or whatever appropriate action you need.