ios 5 change the background of back button in navigation controller to transparent
Asked Answered
A

1

6

I have customised the navigation controller title bar with a background image, but I am really struggling to change the background color of the back button to transparent so that it matches with the green title bar beneath it. I am fairly new to iOS development. Can any one suggest what could be done?

Here is the code for I used to change the title bar of navigation controller, just in case it helps:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    if ([self.navigationController.navigationBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)] ) {
        UIImage *image = [UIImage imageNamed:@"greenbar.png"];
        [self.navigationController.navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
       // [[UIBarButtonItem appearance] setBackButtonBackgroundImage:image forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];

    } 

    //change back button image

}

EDIT: After doing a bit of research I managed to get what I wanted. Here is the code to change the background image for the back button:

 UIImage *image1 = [UIImage imageNamed:@"back-bt.png"];
    [[UIBarButtonItem appearance] setBackButtonBackgroundImage:image1 forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];

The above code adds the image to all the back buttons in the navigation controller.

Apparatus answered 8/5, 2012 at 10:14 Comment(1)
Your solution won't work when the navigation controller is presented in a popover controller. The reason for that is that the navigation bar's background and back bar button item's background in the popover are drawn differently.Jasper
S
5

You can't change the appearance of the default back button, but you can create your own button to replace it.

- (void)viewDidLoad {

    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    if ([self.navigationController.navigationBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)] ) {
        UIImage *image = [UIImage imageNamed:@"greenbar.png"];
        [self.navigationController.navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
       // [[UIBarButtonItem appearance] setBackButtonBackgroundImage:image forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];

    } 

    //change back button image
    if(self.navigationController.viewControllers.count > 1) {
        UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];

        [backButton setTitle:@"Back" forState:UIControlStateNormal];
        [backButton addTarget:self action:@selector(didTapBackButton:) forControlEvents:UIControlEventTouchUpInside];
        backButton.frame = CGRectMake(0.0f, 0.0f, 64.0f, 41.0f);
        UIBarButtonItem *backButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];

        self.navigationItem.leftBarButtonItem = backButtonItem;
    }
}



- (void) didTapBackButton:(id)sender {
    if(self.navigationController.viewControllers.count > 1) {
        [self.navigationController popViewControllerAnimated:YES];
    }
}
Stooge answered 8/5, 2012 at 10:20 Comment(2)
thanks for the answer! It is working, but only for one level deep and not for every view. But is there no other way inbuilt? thanks!Apparatus
no other way that I know of. If you want to include this in all your view controllers create an BaseViewController class that implements this on the viewDidLoad method and use it as a base class for all your view controllers in the app. Just be careful when overriding viewDidLoad in your main view controllers to call the [super viewDidLoad].Stooge

© 2022 - 2024 — McMap. All rights reserved.