Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 735
akaii Avatar asked May 07 '10 03:05

akaii


3 Answers

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

like image 101
TwoBeerGuy Avatar answered Nov 02 '22 15:11

TwoBeerGuy


imageInsets property of UIBarItem?

like image 3
SG1 Avatar answered Nov 02 '22 16:11

SG1


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.

like image 1
akaii Avatar answered Nov 02 '22 15:11

akaii