iOS按钮封装

来源:互联网 发布:ip地址代理软件 编辑:程序博客网 时间:2024/05/22 06:52
+ (UIButton *)buttonWithTitle:(NSString *)title frame:(CGRect) frame target:(id)target action:(SEL)action backgroundImage:(NSString *)backgroundImage selectedImage:(NSString *)selectedImage;

使用扩展

#import "UIButton+Extension.h"@implementation UIButton (Extension)+ (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;}@end
0 0
原创粉丝点击