先建一个页面继承自UIButton
//.h文件
#import <UIKit/UIKit.h>
@interface MyButton : UIButton
+ (UIButton *)buttonWithTitle:(NSString *)title frame:(CGRect) frame target:(id)target action:(SEL)action backgroundImage:(NSString *)backgroundImage selectedImage:(NSString *)selectedImage;
@end
//.m文件
#import "MyButton.h"
@implementation MyButton
+ (UIButton *)buttonWithTitle:(NSString *)title frame:(CGRect) frame target:(id)target action:(SEL)action backgroundImage:(NSString *)backgroundImage selectedImage:(NSString *)selectedImage {
// 创建一个按钮
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
// 设置按钮背景颜色
// button.backgroundColor = [UIColor colorWithRed:0.601 green:0.596 blue:0.906 alpha:1.000];
// 设置按钮大小
button.frame = frame;
// 设置背景图片
[button setBackgroundImage:[UIImage imageNamed:backgroundImage] forState:UIControlStateNormal];
// 设置背景选择图片
[button setBackgroundImage:[UIImage imageNamed:selectedImage] forState:UIControlStateHighlighted];
// 设置按钮的标题
[button setTitle:title forState:UIControlStateNormal];
// 设置按钮的点击事件
[button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
return button;
}