rightBarButtonItem info button, no space to the right
Asked Answered
L

2

9

I have a UIViewController set up to display an info-button on the right in its UINavigationItem like this:

UIButton *infoButton = [UIButton buttonWithType:UIButtonTypeInfoLight];
[infoButton addTarget:self action:@selector(toggleAboutView) forControlEvents:UIControlEventTouchUpInside]; 
self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:infoButton] autorelease];

That works perfectly, but the only problem is that I'm left with little to no space to the right of the button, which looks pretty ugly:

Infobutton

Is there any way for the button to move about 5 px out from the right side?

Thanks.

Lalla answered 11/9, 2010 at 18:8 Comment(3)
It looks properly spaced to me. Look at other iPhone apps that put a button on the right side of a nav bar: Messages, Calendar, Contacts, Maps, etc, etc. They are all spaced the same way.Preoccupy
My client wants it to be further out, and this spacing does not look good with a info-button.Lalla
I use the same button on a nav bar, but on the left instead of the right, using the default spacing. It looks fine to me. If your client wants it jutting out more, ask why they want their app to look completely different from all the other apps that use an info button in the nav bar.Preoccupy
K
17
infoButton.contentEdgeInsets = UIEdgeInsetsMake(0, -10, 0, 10);
Karakoram answered 28/9, 2010 at 9:20 Comment(0)
C
14

Try setting infoButton.contentEdgeInsets (or imageEdgeInsets); something like

infoButton.contentEdgeInsets = (UIEdgeInsets){.right=10};

I'm not sure if it works for "system" buttons, but it might.

If that doesn't work, the easiest way is probably to stick it inside a UIView.

EDIT: Presumably the toolbar calls [customView sizeThatFits:blah] to find out how big the view "wants to be". The above code is supposed to mean "add a 10-pixel right edge inset", which for a normal (custom) button also makes the size bigger by 10 pixels.

It seems like UIButton returns a fixed size for the standard info button, ignoring any insets. Your code "works" by affecting the contentRectForBounds: and imageRectForContentRect: methods, which changes where the icon is drawn, but does not actually change the size of the button. I suspect that the icon is drawing outside the button, which may mean that the "touchable" rect is wrong (but toolbars seem to increase the touch rects for normal UIBarButtonItems, so maybe it doesn't matter).

Corwin answered 12/9, 2010 at 6:14 Comment(2)
Hah, that worked! I have no idea why, but it worked. Care to explain it to me?Lalla
EDIT: This code didn't do it all, I had to add this: infoButton.contentEdgeInsets = (UIEdgeInsets){.left=-10}; as well, or the button would just be squeezed from the right.Lalla

© 2022 - 2024 — McMap. All rights reserved.