UIButton专题讲解
一、UIButton的创建
buttonWithType方法
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
按钮的风格:
typedef enum {
UIButtonTypeCustom = 0, // 自定义,无风格
UIButtonTypeRoundedRect, // 白色圆角矩形
UIButtonTypeDetailDisclosure, // 蓝色的披露按钮,可放在任何文字旁
UIButtonTypeInfoLight, // 使用的小圆圈信息按钮,可以放在任何文字旁
UIButtonTypeInfoDark, // 白色背景下使用的深色圆圈信息按钮
UIButtonTypeContactAdd, // 蓝色加号(+)按钮,可以放在任何文字旁
} UIButtonType;
二、UIButton的属性
1、设置按钮的位置和大小
btn = CGRectMake(100,50,50,30);
第一个参数是指按钮位置的横坐标;
第二个参数是指按钮位置的纵坐标;
第三个参数是指按钮的宽度;
第四个参数是指按钮的高度;
2、设置按钮的标题文字:setTitle方法
[btn setTitle:@"标题" forState:UIControlStateNormal];
3、给按钮添加图片:setImage方法
[btn setImage:[UIImage imageNamed:@"img.png"] forState:UIControlStateNormal];
4、设置按钮标题文字的颜色:setTitleColor方法
[btn setTitleColor:[UIColor blueColor] forStat];
5、设置标题文字字体的大小:setFont 方法
[btn setFont: [UIFont systemFontSize: 14.0]];
6、设置按钮背景颜色:setBackgroundColor
[btn setBackgroundColor: [UIColor blueColor]];
三、将按钮添加到UIView上
[self.view addSubView :btn];
四、给按钮增加点击事件
[btn addTarget:self action:@selector(btnPressed:) forControlEvents:UIControlEventTouchUpInside];