Reputation: 2495
I have a little black line under all of my buttons which are in UIControlStateNormal
. Back buttons and Done buttons are not affected.
Here is image:
You can clearly see it, and it's super annoying. Here is how I'm setting all of my images:
[[UIBarButtonItem appearance] setBackgroundImage:[UIImage imageNamed:@"nav-button"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
I'm using the same technique for back and done buttons, which doesn't have issues.
Upvotes: 3
Views: 2011
Reputation: 654
Might not be exactly what you want but this is how i handle this :
+ (id) customButtonWithImageNamed:(NSString *)imageName selectedImageNamed:(NSString *)selectedImageName leftCapWidth:(CGFloat)leftCapWidth edgeInsets:(UIEdgeInsets)edgeInsets title:(NSString *)title target:(id)target selector:(SEL)selector {
UIButton* customButton = [UIButton buttonWithType:UIButtonTypeCustom];
[customButton addTarget:target action:selector forControlEvents:UIControlEventTouchUpInside];
customButton.titleLabel.font = [UIFont boldSystemFontOfSize:12.0f];
customButton.titleLabel.shadowColor = [UIColor colorWithRed:0.0f/255.0f green:0.0f/255.0f blue:0.0f/255.0f alpha:0.25f];
customButton.titleLabel.shadowOffset = CGSizeMake(0.0f, -1.0f);
customButton.titleLabel.lineBreakMode = UILineBreakModeTailTruncation;
customButton.titleEdgeInsets = edgeInsets;
UIImage* navButtonBackgroundImage = [[UIImage imageNamed:imageName] stretchableImageWithLeftCapWidth:leftCapWidth topCapHeight:0.0f];
UIImage* navButtonPressedBackgroundImage = [[UIImage imageNamed:selectedImageName] stretchableImageWithLeftCapWidth:leftCapWidth topCapHeight:0.0f];
[customButton setBackgroundImage:navButtonBackgroundImage forState:UIControlStateNormal];
[customButton setTitle:title forState:UIControlStateNormal];
[customButton setBackgroundImage:navButtonPressedBackgroundImage forState:UIControlStateHighlighted];
[customButton setBackgroundImage:navButtonPressedBackgroundImage forState:UIControlStateSelected];
CGSize size = CGSizeMake(30.0f, 30.0f);
if (title != nil) {
size = [[NSString stringWithString:title] sizeWithFont:customButton.titleLabel.font];
}
customButton.frame = CGRectMake(0.0f, 0.0f, size.width + 20.0f, 30.0f);
customButton.layer.shouldRasterize = YES;
customButton.layer.rasterizationScale = [[UIScreen mainScreen] scale];
return [[[UIBarButtonItem alloc] initWithCustomView:customButton] autorelease];
}
I have a static method that returns a nice button for me :)
ANd
+ (id) customBarButtonWithTitle:(NSString *)title target:(id)target selector:(SEL)selector {
return [self customButtonWithImageNamed:@"navbarbutton"
selectedImageNamed:@"navbarbutton_active"
leftCapWidth:5.0f
edgeInsets:UIEdgeInsetsMake(0.0f, 5.0f, 0.0f, 5.0f)
title:title
target:target
selector:selector];
}
=> self.navigationItem.leftBarButtonItem = [UIBarButtonItem customBarButtonWithTitle:@"Info" target:self selector:@selector(pushInfoViewController)];
Upvotes: 2
Reputation: 17500
Try using [[UIImage imageNamed:@"nav-button"] resizableImageWithCapInsets:...]
. Seems like your image is getting tiled because it's shorter than the button height.
Upvotes: 3