I'd like to change the back button of my UINavigationBar
I can do it using this code:
// Set the custom back button
UIImage *buttonImage = [UIImage imageNamed:@"backag.png"];
//create the button and assign the image
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:buttonImage forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:@"selback.png"] forState:UIControlStateHighlighted];
button.adjustsImageWhenDisabled = NO;
//set the frame of the button to the size of the image (see note below)
button.frame = CGRectMake(0, 0, 30, 30);
[button addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
//create a UIBarButtonItem with the button as a custom view
UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithCustomView:button];
self.navigationItem.leftBarButtonItem = customBarItem;
self.navigationItem.hidesBackButton = YES;
// Cleanup
[customBarItem release];
but I have to put that code in each viewDidLoad method. I'd like to do it once for the whole program.
My attempt is this:
UIImage *buttonBack30 = [[UIImage imageNamed:@"backag"] //16 5
resizableImageWithCapInsets:UIEdgeInsetsMake(1000, 1000, 1000, 1000)];
UIImage *buttonBack31 = [[UIImage imageNamed:@"selback"]
resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
[[UIBarButtonItem appearance] setBackButtonBackgroundImage:buttonBack30
forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
//[[UIBarButtonItem appearance] setBackButtonBackgroundImage:buttonBack31
// forState:UIControlStateSelected barMetrics:UIBarMetricsDefault];
[[UIBarButtonItem appearance] setBackButtonBackgroundImage:buttonBack31
forState:UIControlStateHighlighted barMetrics:UIBarMetricsDefault];
NSMutableDictionary *attributes = [NSMutableDictionary dictionary];
[attributes setValue:[UIColor colorWithRed:(163.0f/255.0f) green:(0.0f) blue:(0.0f) alpha:1.0f] forKey:UITextAttributeTextColor];
[attributes setValue:[UIColor clearColor] forKey:UITextAttributeTextShadowColor];
// [attributes setValue:[UIFont fontWithName:@"Helvetica" size:15] forKey:UITextAttributeFont];
[attributes setValue:[NSValue valueWithUIOffset:UIOffsetMake(0.0, 0.0)] forKey:UITextAttributeTextShadowOffset];
[[UIBarButtonItem appearance] setTitleTextAttributes:attributes forState:UIControlStateHighlighted];
but it stretches out the image and draws text over it, which I don't want. I just want an image that is the same size in each view.
Thanks!
This might work out. Add the below in ur appdelegate in the top.
and call
[self setNavigationBar];
in yourviewDidLoad
use this code and enjoy...
Hope, this will help you..
Here is working solution for Swift
You Can create a custom button for the backBarButtonItem, and then customize it.
go to this question - How do you change color/image on the default backBarButtonItem? and you can find your answer there.
For making a bar button item look like a backBarButtonItem, refer to this answer. Creating a left-arrow button (like UINavigationBar's "back" style) on a UIToolbar
Starting with iOS 5.0, you can use the global Appearance modifiers to change all back buttons throughout your app:
The background image must be a resizable image for good results.