Reputation: 1576
I'm using NavigationBar for my application . in iOS7, my navigation leftButtonItem icon appears fine(http://prntscr.com/2vaha7). But on iOS6 , it looks like that : http://prntscr.com/2vafer.
My code :
_item2 = [[UINavigationItem alloc] init];
_item2.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"sliderMenu.png"] style:UIBarButtonItemStylePlain target:self action:@selector(revealMenu:)];
[self.navigationBar pushNavigationItem:_item2 animated:NO];
How can i fix this visual issue instead of works also fine in iOS6 ?
Best Regards,
Onder.
Upvotes: 0
Views: 173
Reputation: 916
look at : http://www.raywenderlich.com/21703/user-interface-customization-in-ios-6
and : http://iosdevblog.com/2013/01/26/the-recommended-size-for-custom-uibarbuttonitem/
you will need to create a custom transparent back image for your button, and set it as the background image of bar button for ios 6 :
[[UIBarButtonItem appearance] setBackgroundImage:clearImage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[[UIBarButtonItem appearance] setBackgroundImage:clearImage forState:UIControlStateHighlighted barMetrics:UIBarMetricsDefault];
//edit : i firstly thought it was a back button, which you need to mak resizable like the following:
UIImage *buttonBack30 = [clearImage resizableImageWithCapInsets:UIEdgeInsetsMake(0, 10, 0, 6)];
[[UIBarButtonItem appearance] setBackButtonBackgroundImage:buttonBack30 forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
I created clearImage with the following code, but you can simply create a 34x60 image of any color :
+ (UIImage *)imageWithColor:(UIColor *)color andSize:(CGRect)rect {
// Create a 1 by 1 pixel context
UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0);
[color setFill];
UIRectFill(rect); // Fill it with your color
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
UIImage *clearImage = [self imageWithColor:[UIColor clearColor] andSize:CGRectMake(0, 0, 34, 60)];
[[UIBarButtonItem appearance] setBackgroundImage:backgroundImage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[[UIBarButtonItem appearance] setBackgroundImage:backgroundImage forState:UIControlStateHighlighted barMetrics:UIBarMetricsDefault];
Upvotes: 2