必須是自定義的按鈕類型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