Change the width of an UIBarButtonItem
Solution 1
you cant. If you want custom width you will need to use initWithCustomView
Solution 2
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;
gnclmorais
Computer engineer turned front-end developer, currently working in London. I'm interested in everything. Seriously. I'd gladly spend my entire life learning about everything. But I can say that recently I've been specially interested in server-side JavaScript, cloud services, and life hacks & tricks. Oh, and chocolate & coffee.
Updated on July 29, 2022Comments
-
gnclmorais almost 2 years
How can I change the width of an
UIBarButtonItem
to 29px? The property width does not work and I don't want to create aUIButton
and use theinitWithCustomView
because I want the square background to adjust to the navigation bar color. I'm using theinitWithImage
to init theUIBarButtonItem
.Here is my button with a current width of 37px. I want to set to 29px.
-
gnclmorais over 12 yearsBut why do they have the property width in the UIBarButtonItem class?
-
RolandasR over 12 yearsyou can use it only if button in UIToolbar