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

akaii picture akaii · May 7, 2010 · Viewed 12.1k times · Source

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.

Answer

TwoBeerGuy picture TwoBeerGuy · May 18, 2010

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).