mars mars - 1 year ago 88
Objective-C Question

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!

Answer Source

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'

[self.navigationController popViewControllerAnimated:YES];


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download