Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 260
mars Avatar asked Jun 10 '15 13:06

mars


People also ask

How do I customize the back button on my iPhone?

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.

How do I change the appearance of my navigation bar?

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.


2 Answers

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.

Custom back image in navigation bar

like image 136
Badal Shah Avatar answered Oct 08 '22 19:10

Badal Shah


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];
}
like image 22
mars Avatar answered Oct 08 '22 18:10

mars