use [UINavigationBar appearance] to change back bu

2019-05-26 03:36发布

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!

5条回答
萌系小妹纸
2楼-- · 2019-05-26 03:45

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

查看更多
beautiful°
3楼-- · 2019-05-26 03:45

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..

查看更多
姐就是有狂的资本
4楼-- · 2019-05-26 03:49

Here is working solution for Swift

    let backImage = UIImage(named: "back")
    UINavigationBar.appearance().backIndicatorImage = backImage
    UINavigationBar.appearance().backIndicatorTransitionMaskImage = backImage
    UIBarButtonItem.appearance().setTitleTextAttributes([NSAttributedString.Key.foregroundColor:
        UIColor.white], for: .normal)
    UIBarButtonItem.appearance().tintColor = UIColor.green //if you want to change color
查看更多
Anthone
5楼-- · 2019-05-26 03:50

You Can create a custom button for the backBarButtonItem, and then customize it.

go to this question - How do you change color/image on the default backBarButtonItem? and you can find your answer there.

For making a bar button item look like a backBarButtonItem, refer to this answer. Creating a left-arrow button (like UINavigationBar's "back" style) on a UIToolbar

查看更多
孤傲高冷的网名
6楼-- · 2019-05-26 04:03

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.

查看更多
登录 后发表回答