use [UINavigationBar appearance] to change back button image

19,236

Solution 1

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

Solution 2

Starting with iOS 5.0, you can use the global Appearance modifiers to change all back buttons throughout your app:

[[UIBarButtonItem appearance] setBackButtonBackgroundImage:[UIImage imageNamed:@"back_button_bg"]
                                        forState:UIControlStateNormal
                                      barMetrics:UIBarMetricsDefault];

The background image must be a resizable image for good results.

Solution 3

use this code and enjoy...

UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
[leftButton setUserInteractionEnabled:NO];
[leftButton setImage:[UIImage imageNamed:@"backag.png"] forState:UIControlStateNormal];    
leftButton.frame = CGRectMake(0, 0, 30, 30);
[leftButton addTarget:self action:@selector(YourclickeventClick:) forControlEvents:UIControlEventTouchUpInside];        
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
[leftbutton release];

Hope, this will help you..

Share:
19,236
itgiawa
Author by

itgiawa

Updated on June 04, 2022

Comments

  • itgiawa
    itgiawa almost 2 years

    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!

  • itgiawa
    itgiawa about 12 years
    that works, but the button blinks when I move back and forth between views. Not that big of a deal, but it would be nice if it could transition cleaner
  • Varun Naharia
    Varun Naharia over 8 years
    and where is the back action method ?
  • Amr Lotfy
    Amr Lotfy about 7 years
    Nice hack!, back method swift self.navigationController?.popViewController(animated: true)