How do you adjust the frame or vertical alignment of a UIBarButtonItem contained by a UIToolbar instance?
Asked Answered
B

3

25

Horizontal positioning of UIBarButtonItems is no problem, I can simply pad the space with fixed/flexible space items. However, I can't seem to adjust the toolbar item vertically. UIToolbar has no alignment property, and UIBarButtonItem has no way of setting its frame.

I need to do this because we're using a mix of custom icons created using initWithImage, and standard icons created with initWithBarButtonSystemItem. The custom icons aren't being centered properly (they're offset upwards, relative to the system icons, which are centered properly), so the toolbar looks awkward.

Bankroll answered 7/5, 2010 at 3:29 Comment(3)
I recommend you either 1) use something custom and not a toolbar; or 2) fix the images as they will be much easier to properly adjust.Adynamia
1 is not an option for me. I do not get to make visual design decisions. I don't get to decide how the UI looks, what the effects for the buttons are, or where they are located. 2 is something I would rather avoid if possible, since it means passing back work up to the visual designer. It's tedious to repeatedly pass the images back and forth, to tweak it and then examine the results through trial and error. There also isn't any guarantee that this will fix the problem... Even if we resize the icon, UIKit might still shift it up. Any other recommendations?Bankroll
Any ideas on how to do it for text? The imageinsets solution doesn't work for text.Reube
D
34

Yes, imageInsets worked fine for me!

 float topInset = 4.0f;
 myUIBarButton.imageInsets = UIEdgeInsetsMake(topInset, 0.0f, -topInset, 0.0f); 

moves the image 4 pixels down.

AVOID THIS:

 float topInset = 4.0f;
 myUIBarButton.imageInsets = UIEdgeInsetsMake(topInset, 0.0f, 0.0f, 0.0f); 

moves the image 4 pixels down (BUT rescales the image height so it looks compressed).

Danas answered 18/5, 2010 at 8:19 Comment(1)
Glad to see I'm not the only one with this issue. Although, "topInset = 2.0f" worked for me.Distrait
B
3

imageInsets property of UIBarItem?

Beverle answered 9/5, 2010 at 3:45 Comment(0)
B
1

Doesn't seem to be any simple, clean way of doing this, so I went with an ugly but functional hack: nesting. I stuck another UIToolbar containing the button into a UIView which I set as a UIBarButtonItem on the original toolbar using initWithCustomView. The second UIToolbar can move freely within the UIView, and the actual icon retains all the properties of a UIBarButtonItem.

Ugh.

Bankroll answered 7/5, 2010 at 7:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.