views:

470

answers:

3

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.

+1  A: 

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.

akaii
+1  A: 

imageInsets property of UIBarItem?

SG1
A: 

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

TwoBeerGuy