mars
mars

Reputation: 1641

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!

Upvotes: 5

Views: 11615

Answers (6)

Gulz
Gulz

Reputation: 1813

for swift 4 this worked for me just fine:

        let image = #imageLiteral(resourceName: "ic_navbar_back") //put your image here
        let backButtonImage = image.withRenderingMode(.alwaysOriginal)
        let appearance = UINavigationBar.appearance()
        appearance.backIndicatorImage = backButtonImage
        appearance.backIndicatorTransitionMaskImage = backButtonImage
        appearance.titleVerticalPositionAdjustment(for: .default)

you are going to put this code into appdelegate, within method func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool before the return line

Upvotes: 1

mars
mars

Reputation: 1641

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];
}

Upvotes: 5

Badal Shah
Badal Shah

Reputation: 7612

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

Upvotes: 16

Kiran Patil
Kiran Patil

Reputation: 432

You need to put your code into

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

it will work properly.

Upvotes: 2

Quentin Hayot
Quentin Hayot

Reputation: 7876

Make sure to call your UI related methods in the main thread:

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

Upvotes: 0

Ilesh P
Ilesh P

Reputation: 4046

Try to set UIBarButtonItem like 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 an original post in apple Dev center discussion Forums

For supporting both version iOS7 as well as lower then you check system-version and set code like:-

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];
 }

Upvotes: 1

Related Questions