必须是自定义的按钮类型spa
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];.net
UIImage *normal = [UIImage resizableImage:@"RedButton"];3d
UIImage *high = [UIImage resizableImage:@"RedButtonPressed"];orm
[button setBackgroundImage:normal forState:UIControlStateNormal];图片
[button setBackgroundImage:high forState:UIControlStateHighlighted];get
#import <UIKit/UIKit.h>it
@interface UIImage (Extension)io
+ (UIImage *)resizableImage:(NSString *)name;class
@end import
#import "UIImage+Extension.h"
@implementation UIImage (Extension)
/**
* 返回一张能够随意拉伸不变形的图片
*
* @param name 图片名字
*/
+ (UIImage *)resizableImage:(NSString *)name
{
UIImage *normal = [UIImage imageNamed:name];
CGFloat w = normal.size.width * 0.5;
CGFloat h = normal.size.height * 0.5;
return [normal resizableImageWithCapInsets:UIEdgeInsetsMake(h, w, h, w)];
}
@end