Change the width of an UIBarButtonItem [duplicate]

2019-02-17 00:21发布

This question already has an answer here:

How can I change the width of an UIBarButtonItem to 29px? The property width does not work and I don't want to create a UIButton and use the initWithCustomView because I want the square background to adjust to the navigation bar color. I'm using the initWithImage to init the UIBarButtonItem.

Here is my button with a current width of 37px. I want to set to 29px.

UIButton

2条回答
干净又极端
2楼-- · 2019-02-17 00:45

you cant. If you want custom width you will need to use initWithCustomView

查看更多
淡お忘
3楼-- · 2019-02-17 00:49

You can do it by dropping an image to the bar button item from the interface builder and changing the width of the custom view with this code:

CGRect frame = self.navigationItem.leftBarButtonItem.customView.frame;
frame.size.width = 141;
self.navigationItem.leftBarButtonItem.customView.frame = frame;
查看更多
登录 后发表回答