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

浪子不回头ぞ 提交于 2019-11-30 06:54:21
Sergiy Salyuk

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];
Molotoff

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

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