概述
先建一个页面继承自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;
}
最后
以上就是霸气火为你收集整理的自定义UIButton的全部内容,希望文章能够帮你解决自定义UIButton所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复