Ios – how to set image on default back button of navigation bar


I am setting back button image on navigation bar for all the view controllers using the following code in Appdelegate:

    [[UIBarButtonItem appearance] setBackButtonBackgroundImage:image forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];

Now in one of my view controllers i pick a newimage from the gallery and save it. I want that as soon as i save this newimage, the 'image' of back button gets replaced with this 'newimage'.

I tried following code in viewWillAppear of each and every view controller

    [self.navigationItem.backBarButtonItem setBackButtonBackgroundImage:newimage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];

and tried

 [self.navigationItem.backBarButtonItem setImage:new];

this too. But all in vain.
Image gets changed only when i run my app again and thus when code in Appdelegate gets called.

Plz help!

Best Solution

For set own back image button you need to set custom appearance . and call in appdelegate.h file.

Note: You need to set once in appdelegate.h and then it is apply in whole project. don't need to declare in each and every controller.

please check this.

   //in Appdelegate.h file
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
[self customappearance]; //Method declaration

and declare your image with image name in custom appearance. Like this,

//int imageSize = 20;
UIImage *myImage = [UIImage imageNamed:@"icon_back"]; //set your backbutton imagename
UIImage *backButtonImage = [myImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
// now use the new backButtomImage
[[UINavigationBar appearance] setBackIndicatorImage:backButtonImage];
[[UINavigationBar appearance] setBackIndicatorTransitionMaskImage:backButtonImage];

 UIImage *backbutton=[[UIImage imageNamed:@"icon_back"]resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0) resizingMode:UIImageResizingModeStretch];

 //resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0) resizingMode:UIImageResizingModeStretch];

 [[UIBarButtonItem appearance] setBackButtonBackgroundImage:backbutton forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];*/
[[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(-400.f, 0)


and your navigationbar back button look like this.

Custom back image in navigation bar