2011-12-27 91 views
8

如何将UIBarButtonItem的宽度更改为29px?属性宽度不起作用,我不想创建UIButton并使用initWithCustomView,因为我想让方形背景适应导航栏的颜色。 我正在使用initWithImage来初始化UIBarButtonItem更改UIBarButtonItem的宽度

这是我的按钮,当前宽度为37px。我想设置为29px。

UIButton

+0

@MaxMacLeod提问者明确不想使用initWithCustomView ... – 2013-07-21 02:36:25

回答

16

你不能。如果你想定制的宽度,你将需要使用initWithCustomView

+2

但是他们为什么在UIBarButtonItem类中有属性宽度? – gnclmorais 2011-12-28 10:02:33

+12

只有在UIToolbar中有按钮时才能使用它 – RolandasR 2011-12-28 10:36:57

6

您可以将图像从界面生成器拖放到栏按钮项目并更改自定义视图的宽度与此代码做到这一点:

CGRect frame = self.navigationItem.leftBarButtonItem.customView.frame; 
frame.size.width = 141; 
self.navigationItem.leftBarButtonItem.customView.frame = frame;