Search code examples
iphoneuinavigationbarback-buttonuinavigationitem

use [UINavigationBar appearance] to change back button image


I'd like to change the back button of my UINavigationBar

I can do it using this code:

 // Set the custom back button
    UIImage *buttonImage = [UIImage imageNamed:@"backag.png"];

    //create the button and assign the image
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setImage:buttonImage forState:UIControlStateNormal];
    [button setImage:[UIImage imageNamed:@"selback.png"] forState:UIControlStateHighlighted]; 
    button.adjustsImageWhenDisabled = NO;


    //set the frame of the button to the size of the image (see note below)
    button.frame = CGRectMake(0, 0, 30, 30);

    [button addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];

    //create a UIBarButtonItem with the button as a custom view
    UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithCustomView:button];
    self.navigationItem.leftBarButtonItem = customBarItem;
    self.navigationItem.hidesBackButton = YES;

    // Cleanup
    [customBarItem release];

but I have to put that code in each viewDidLoad method. I'd like to do it once for the whole program.

My attempt is this:

 UIImage *buttonBack30 = [[UIImage imageNamed:@"backag"] //16 5
                             resizableImageWithCapInsets:UIEdgeInsetsMake(1000, 1000, 1000, 1000)];
    UIImage *buttonBack31 = [[UIImage imageNamed:@"selback"] 
                             resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
    [[UIBarButtonItem appearance] setBackButtonBackgroundImage:buttonBack30 
                                                      forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
    //[[UIBarButtonItem appearance] setBackButtonBackgroundImage:buttonBack31 
    //                forState:UIControlStateSelected barMetrics:UIBarMetricsDefault];
    [[UIBarButtonItem appearance] setBackButtonBackgroundImage:buttonBack31 
                                                      forState:UIControlStateHighlighted barMetrics:UIBarMetricsDefault];

    NSMutableDictionary *attributes = [NSMutableDictionary dictionary];
    [attributes setValue:[UIColor colorWithRed:(163.0f/255.0f) green:(0.0f) blue:(0.0f) alpha:1.0f] forKey:UITextAttributeTextColor];
    [attributes setValue:[UIColor clearColor] forKey:UITextAttributeTextShadowColor];
    // [attributes setValue:[UIFont fontWithName:@"Helvetica" size:15] forKey:UITextAttributeFont];
    [attributes setValue:[NSValue valueWithUIOffset:UIOffsetMake(0.0, 0.0)] forKey:UITextAttributeTextShadowOffset];
    [[UIBarButtonItem appearance] setTitleTextAttributes:attributes forState:UIControlStateHighlighted];

but it stretches out the image and draws text over it, which I don't want. I just want an image that is the same size in each view.

Thanks!


Solution

  • This might work out. Add the below in ur appdelegate in the top.

        @implementation UIViewController (CustomFeatures)
    -(void)setNavigationBar{
        // Set the custom back button
        UIImage *buttonImage = [UIImage imageNamed:@"backag.png"];
    
        //create the button and assign the image
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setImage:buttonImage forState:UIControlStateNormal];
        [button setImage:[UIImage imageNamed:@"selback.png"] forState:UIControlStateHighlighted]; 
        button.adjustsImageWhenDisabled = NO;
    
    
        //set the frame of the button to the size of the image (see note below)
        button.frame = CGRectMake(0, 0, 30, 30);
    
        [button addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    
        //create a UIBarButtonItem with the button as a custom view
        UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithCustomView:button];
        self.navigationItem.leftBarButtonItem = customBarItem;
        self.navigationItem.hidesBackButton = YES;
    
        // Cleanup
        [customBarItem release];
    }
    @end
    

    and call [self setNavigationBar]; in your viewDidLoad