UIButton : 是iOS中用于响应用户点击事件的空间 他和UILable类似 只不过多了事件的处理功能, 在使用上和UILable差别不大
(1) 创建空间对象 UIButtonTypeCustom 可以让button上的图片显示原来的颜色
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
2 配置属性
button.frame = CGRectMake(20, 80, 250, 250);
// button.backgroundColor = [UIColor cyanColor];
//(1)设置title
//正常状态下 title的设置
// [button setTitle:@"登陆" forState:UIControlStateNormal];
//高亮状态下 title的设置
// [button setTitle:@"登陆中....." forState:UIControlStateHighlighted];
(2)添加点击事件
[button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
[button addTarget:self action:@selector(buttonAction2:) forControlEvents:UIControlEventTouchDown];
(3)移除点击事件
[button removeTarget:self action:@selector(buttonAction2:) forControlEvents:UIControlEventTouchDown];
(4)设置正常状态title的颜色
[button setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
设置高亮状态title的颜色
[button setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted];
(5) 设置正常状态阴影的颜色
[button setTitleShadowColor:[UIColor redColor] forState:UIControlStateNormal];
高亮状态阴影的颜色
[button setTitleShadowColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
(6)设置背景图片
[button setBackgroundImage:[UIImage imageNamed:@"01.png"] forState:UIControlStateNormal];
[button setBackgroundImage:[UIImage imageNamed:@"02.png"] forState:UIControlStateHighlighted];
设置前景图片
// [button setImage:[UIImage imageNamed:@"01.png"] forState:UIControlStateNormal];
// [button setImage:[UIImage imageNamed:@"02.png"] forState:UIControlStateHighlighted];
//3 添加到父视图
[self.window addSubview:button];
// button.adjustsImageWhenHighlighted = NO;
// button.showsTouchWhenHighlighted = YES;
// [button setTag:100];