问题
So, this is what I'm trying to accomplish:
It's a UINavigationBar
with a UIBarButtonItem
that gets initialized with a custom UIButton
.
Basically like this:
UIButton *favoriteButton = [UIButton buttonWithType:UIButtonTypeCustom];
[favoriteButton addTarget:target
action:action
forControlEvents:UIControlEventTouchUpInside];
favoriteButton.frame = CGRectMake(0.0f, 0.0f, 44.0f, 44.0f);
UIImage *backgroundImage = [[UIImage imageNamed:@"favorite-button-background-orange"]
resizableImageWithCapInsets:UIEdgeInsetsMake(0, 2, 0, 0)];
[favoriteButton setBackgroundImage:backgroundImage
forState:UIControlStateNormal];
UIBarButtonItem *favoriteButtonItem = [[UIBarButtonItem alloc] initWithCustomView:favoriteButton];
The problem is I can't seem to get rid of that nasty little border on the right hand side of the UINavigationBar
.
I tried setting a custom TitleView
as described in "How to remove padding next to a UIBarButtonItem" but this didn't change a thing.
I also tried adding another UIBarButtonItem with a negative width (as suggested in some blogposts I read) but that didn't help either.
UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
target:nil
action:nil];
negativeSpacer.width = -5.0f;
self.navigationItem.rightBarButtonItems = %[favoriteButtonItem, negativeSpacer];
I assume iOS won't let me change it because the "rounded border" reserves some (unavoidable?) space / padding?
So my question is. How should I work around this limitation? Do I need to create a custom UINavigationBar
class? Can I fake it somehow by modifying the UIBarButtonItem
? Am I just using the wrong component?
回答1:
There would be two solutions. (It may not be exact solution)
1) You should subclass UINavigationBar
and play around with it. For example you may have to draw your UIBarButtonItem
yourself in the right corner.
2) Instead of using UINavigationBar
, Use UIView
and add subviews to make it exactly look like the UINavigationBar
and use it.
I hope second solution would be easy to achieve in your requirement. I don't think there would be any other way to achieve your requirement.
回答2:
There is no option for customizing the default navigation bar button items(back button, right bar button and left barbutton )
in navigation bar. For getting your requirement you can use custom navigation bar.
回答3:
The second solution you mention actually works (tested today on both iOS6 & 7). The problem is that the elments of the rightBarButtonsItem
array are displayed from right to left; this means that the negative space actually needs to appear as first item of the array to achieve the desired effect.
来源:https://stackoverflow.com/questions/16106306/get-rid-of-the-space-on-the-right-side-of-a-uinavigationbar