UIButton/按钮

UIButton

一、UIButton是一个视图类,继承自UIControl 间接继承自UIView;

凡是UIControl子类可以接收触发事件;

UIButton用于创建一个按钮,添加点击事件。


1、为button设置背景色

button.backgroundColor=[UIColor redColor];

2、为button设置文字

第一个参数是button的文字,第二个参数是state

(1)普通状态下文字

[button setTitle:@"我是普通按钮" forState:UIControlStateNormal];

(2)高亮状态下文字

[button setTitle:@"我是高亮按钮" forState:UIControlStateHighlighted];    

(3)禁用状态下文字

 [button setTitle:@"我是禁用按钮" forState:UIControlStateDisabled];

设置禁用状态属性(默认为YES属性)

 button.enabled=YES;

(4)选中状态下文字

[button setTitle:@"我是选中按钮" forState:UIControlStateSelected];

设置选中时状态属性,默认为NO

button.selected=YES;

UIControlStateNormal       = 0,                   普通状态

UIControlStateHighlighted  = 1 << 0,        高亮状态

UIControlStateDisabled     = 1 << 1,          禁用状态

UIControlStateSelected     = 1 << 2,           选中状态

3、设置按钮的文字颜色

(1)普通状态

[button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];

(2)高亮状态

[button setTitleColor:[UIColor greenColor] forState:UIControlStateHighlighted];

(3)禁用状态

[button setTitleColor:[UIColor grayColor] forState:UIControlStateDisabled];

(4)选中状态

[button setTitleColor:[UIColor blackColor] forState:UIControlStateSelected];

4、设置button的前景图

(1)普通状态

[button setImage:[UIImage imageNamed:@"1.png"] forState:UIControlStateNormal];

(2)高亮状态

[button setImage:[UIImage imageNamed:@"2.png"] forState:UIControlStateHighlighted];

(3)禁用状态

[button setImage:[UIImage imageNamed:@"3.png"] forState:UIControlStateDisabled];

(4)选中状态

[button setImage:[UIImage imageNamed:@"4.png"] forState:UIControlStateSelected];

5、设置button的背景图

(1)普通状态

[button setBackgroundImage:[UIImage imageNamed:@"5.png"] forState:UIControlStateNormal];

(2)高亮状态

[button setBackgroundImage:[UIImage imageNamed:@"6.png"] forState:UIControlStateHighlighted];

(3)禁用状态

[button setBackgroundImage:[UIImage imageNamed:@"7.png"] forState:UIControlStateDisabled];

(4)选中状态

[button setBackgroundImage:[UIImage imageNamed:@“8.png"] forState:UIControlStateSelected];

6、为button添加事件

第一个参数是接收消息的对象,第二个参数是触发事件的方法,第三个参数是触发事件的类型

[button addTarget:self action:@selector(onClick:) forControlEvents:UIControlEventTouchDown];

UIControlEventTouchDown             = 1 <<  0,   点击即触发

UIControlEventTouchDragInside     = 1 <<  2,   点击松开触发

7、把button放到我们的ViewController

[self.view addSubview:button];

8、释放button

[button release];

9、创建一个UIButton(最后需要释放button)

(1)普通方法创建

UIButton *button=[[UIButton alloc] initWithFrame:CGRectMake(50, 50, self.view.frame.size.width-100, 150)];

(2)类方法创建button(不需要释放)

UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];



二、例题

(1)button chess



(2)计算器



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值