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!
Check that you have the latest version of iOS on your iPhone 8 or later. Go to Settings > Accessibility > Touch, and tap Back Tap. Tap Double Tap or Triple Tap and choose an action. Double or triple tap on the back of your iPhone to trigger the action you set.
To change the appearance of the navigation bar: Choose “standard” and “scroll edge appearances” for the navigation bar, by setting the appearance proxy of UINavigationBar : “Standard”, and “ScrollEdge” appearances. Open the project's storyboard file. Select the UINavigationBar from your UINavigationController scene.
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,
-(void)customappearance
{
//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)
forBarMetrics:UIBarMetricsDefault];
}
and your navigationbar back button look like this.
Finally ended up doing this way:
Removed the code from AppDelegate
and added following lines of code in viewWillAppear
of each and every View controller
UIBarButtonItem * barButtonItem = [[UIBarButtonItem alloc] initWithImage:newimage
style:UIBarButtonItemStylePlain
target:self
action:@selector(goBack)];
[self.navigationItem setLeftBarButtonItem:barButtonItem];
Now back button will not work anymore, so its functionality has to be provided explicitly in @selector 'goBack'
- (void)goBack
{
[self.navigationController popViewControllerAnimated:YES];
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With