I'm trying to get the width of a UIBarButtonItem
.
This doesn't work:
barbuttonitem.customView.frame.size.width
And this won't work, either:
barbuttonitem.width
What about this:
UIBarButtonItem *item = /*...*/;
UIView *view = [item valueForKey:@"view"];
CGFloat width = view? [view frame].size.width : (CGFloat)0.0;
I had the same problem. After a lot of tries I found something that worked!
In my specific case, I needed to get the width of the first UIBarButtonItem from the navigation controller's toolbar, but you can easily adapt it to your likings:
UIToolbar *toolbar = self.navigationController.toolbar; // or whatever toolbar you need
UIView *view = (UIView *)[toolbar.subviews objectAtIndex:0]; // 0 for the first item
double itemWidth = view.bounds.size.width;
Please note: I had to use this code in viewDidLoad to get a proper value. In the init it returns 0.0
Arthur
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With