一、UIButton
作用:响应用户的点击事件
常用属性
二、按钮的状态
UIButton的状态
UIControlStateNormal //正常状态
UIControlStateHighlighted //高亮状态
UIControlStateDisabled //禁用状态
UIControlStateSelected //选中状态
UIControlStateApplication
UIControlStateReserved
三、示例代码
创建UIButton并设置其属性
<pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"> self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor purpleColor];
//工厂方法
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];<span style="font-family: Arial, Helvetica, sans-serif;"> </span>
//禁用按钮
button.enabled = NO;
//按钮选中
button.selected = YES; //设置标题,状态正常 [button setTitle:@"normal" forState:UIControlStateNormal]; //设置标题,状态高亮 [button setTitle:@"highligh" forState:UIControlStateHighlighted]; //设置标题,状态禁用 [button setTitle:@"disable" forState:UIControlStateDisabled]; //设置标题,状态选中
[button setTitle:@"select" forState:UIControlStateSelected];
<span style="font-family: Arial, Helvetica, sans-serif;">//设置title的颜色</span>
[button setTitleColor:[UIColor redColor] forState:<span style="font-family: Arial, Helvetica, sans-serif;">forState:UIControlStateNormal</span>]
//设置frame,与按钮的类型
button.frame = CGRectMake(90, 100, 140, 40);
[self.window addSubview:button];
[self.window makeKeyAndVisible];
return YES;
//设置背景图片
[button setBackgroundImage:[UIImage imageNamed:@"love_normal"] forState:UIControlStateNormal];
[button setBackgroundImage:[UIImage imageNamed:@"love_normal"] forState:UIControlStateSelected];
//设置图片
[button setImage:[UIImage imageNamed:@"love_normal"] forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:@"love_normal"] forState:UIControlStateSelected];