Customizing UIBarButtonItem “Done” style and “Plain” style separately using UIAppearance

岁酱吖の 提交于 2019-12-30 01:54:08

问题


I know how to customize UIBarButtonItem using -setBackgroundImage: forState: barMetrics:, but I would like to use different images for UIBarButtonItemStyleDone and UIBarButtonItemStylePlain.

Is there a way to accomplish this using the UIAppearance protocol? Or do I have to set the image each time I want a "Done" style button?

(I tried messing around with code like the following:

[[UIBarButtonItem appearance] setBackgroundImage:image forState:UIControlStateNormal barMetrics:UIBarButtonItemStyleDone];

But that just sets every bar button with the "Done" image.)

Thanks!


回答1:


In iOS 6 you can use the new method of UIBarButtonItem class:

- (void)setBackgroundImage:(UIImage *)backgroundImage
                  forState:(UIControlState)state
                     style:(UIBarButtonItemStyle)style
                barMetrics:(UIBarMetrics)barMetrics

It sets the background image for the specified state, style, and metrics. More details are available in the Apple docs

So to change the appearance of all UIBarButtonItems you can use something like:

UIImage *doneBackgroundImage = [[UIImage imageNamed:@"button_done.png"]
   resizableImageWithCapInsets:UIEdgeInsetsMake(0, 4, 0, 4)];

[[UIBarButtonItem appearance] setBackgroundImage:doneBackgroundImage
                                            forState:UIControlStateNormal
                                               style:UIBarButtonItemStyleDone
                                          barMetrics:UIBarMetricsDefault];



回答2:


for IOS5*

The only way I found is using a UIBarButtonItem category:

UIBarButtonItem+Appearance.h

#import <Foundation/Foundation.h>

@interface UIBarButtonItem (Appearance)

+ (void) setupAppearance;

@end

UIBarButtonItem+Appearance.m

#import "UIBarButtonItem+Appearance.h"
#import <objc/runtime.h>

@implementation UIBarButtonItem (Appearance)

+ (void) setupAppearance {
    [[UIBarButtonItem appearance]  setBackgroundImage: [[UIImage imageNamed:@"customButton"]
                                                                  resizableImageWithCapInsets: UIEdgeInsetsMake(8, 8, 8, 8)]
                                                       forState: UIControlStateNormal
                                                     barMetrics: UIBarMetricsDefault];

    [[UIBarButtonItem appearance]  setBackgroundImage: [[UIImage imageNamed:@"customButtonHiglhighted"]
                                                                  resizableImageWithCapInsets: UIEdgeInsetsMake(8, 8, 8, 8)]
                                                       forState: UIControlStateHighlighted
                                                     barMetrics: UIBarMetricsDefault];



    Class klass = objc_getClass("UIBarButtonItem");
    Method targetMethod = class_getInstanceMethod(klass, @selector(setStyle:));
    Method newMethod = class_getInstanceMethod(klass, @selector(__setStyle:));
    method_exchangeImplementations(targetMethod, newMethod);
}

- (void) __setStyle:(UIBarButtonItemStyle)style {
    [self __setStyle:style];

    if(style == UIBarButtonItemStyleDone) {
        [self setBackgroundImage:[[UIImage imageNamed:@"customDoneButton"] resizableImageWithCapInsets: UIEdgeInsetsMake(8, 8, 8, 8)]
                        forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
        [self setBackgroundImage:[UIImage imageNamed:@"customDoneButtonClicked"]
                        forState:UIControlStateHighlighted barMetrics:UIBarMetricsDefault];
    } else {
        [self setBackgroundImage:[[UIImage imageNamed:@"customButton"] resizableImageWithCapInsets: UIEdgeInsetsMake(8, 8, 8, 8)]
                        forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
        [self setBackgroundImage:[UIImage imageNamed:@"customButtonHighlighted"]
                        forState:UIControlStateHighlighted barMetrics:UIBarMetricsDefault];
    }
}

@end

Hope this is what you are looking for. Credit for this solution goes to https://gist.github.com/2633081



来源:https://stackoverflow.com/questions/12583574/customizing-uibarbuttonitem-done-style-and-plain-style-separately-using-uiap

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!