How to set image to default back button in navigation bar

I set the image of a button on the navigation bar for all view controllers using the following code in Appdelegate:

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

Now in one of my view controllers I select a new image from the gallery and save it. I want that as soon as I save this new image, the "image" of the back button is replaced with this "new image".

I tried the following code in viewWillAppear of each view controller

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

and tried

  [self.navigationItem.backBarButtonItem setImage:new]; 

it is too. But all in vain. The image changes only when I run my application again and, therefore, when the code is called in Appdelegate.

Help Plz!

+6
source share
5 answers

Finally ended up like this:

Removed code from AppDelegate and added the following lines of code to viewWillAppear each view controller

 UIBarButtonItem * barButtonItem = [[UIBarButtonItem alloc] initWithImage:newimage style:UIBarButtonItemStylePlain target:self action:@selector(goBack)]; [self.navigationItem setLeftBarButtonItem:barButtonItem]; 

Now the back button will no longer work, so its functionality must be explicitly specified in @selector 'goBack'

 - (void)goBack { [self.navigationController popViewControllerAnimated:YES]; } 
+5
source

To customize your own reverse image button, you need to set a custom look. and call the appdelegate.h file.

Note. You need to install once in appdelegate.h and then apply it throughout the project. no need to declare in each controller.

please check it.

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

and declare your image with the image in a custom look. 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 back button in the navigation bar looks like this.

Custom back image in navigation bar

+15
source

Try setting UIBarButtonItem this way in ios7: -

 UIImage *temp = [[UIImage imageNamed:@"theImage"] imageWithRenderingMode: UIImageRenderingModeAlwaysOriginal]; UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithImage:temp style:UIBarButtonItemStyleBordered target:self action:@selector(action)]; 

Here is the original post in apple dev center discussion forums

To support both version iOS7 and below, you check the system version and install the code as follows: -

 UIImage *temp=nil; if([[[UIDevice currentDevice] systemVersion] floatValue] < 7.0) { temp = [UIImage imageNamed:@"btn-back.png"]; } else { temp = [[UIImage imageNamed:@"btn-back.png"] imageWithRenderingMode: UIImageRenderingModeAlwaysOriginal]; } 
+1
source

You need to put your code in

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 

It will work correctly.

+1
source

Be sure to call the UI-related methods in the main thread:

 dispatch_async(dispatch_get_main_queue(), ^{ [self.navigationItem.backBarButtonItem setBackButtonBackgroundImage:newimage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault]; }); 
0
source

Source: https://habr.com/ru/post/988852/


All Articles