【UIButton的作用】
UIButton为按钮,继承自UIControl类。使用UIButton可以让用户和APP进行交互
【UIButton的创建】
1)initWithFrame:方法
UIButton *btn1 = [[UIButtonalloc]initWithFrame:CGRectMake(60,60,200,60)];
btn1.backgroundColor = [UIColorredColor];
[btn1 setTitle:@"btn1"forState:UIControlStateNormal];
[self.windowaddSubview:btn1];
2)buttonWithType:方法
UIButton *btn2 = [UIButtonbuttonWithType:UIButtonTypeRoundedRect];
btn2.frame =CGRectMake(60,120,200,60);
btn2.backgroundColor = [UIColoryellowColor];
[btn2 setTitle:@"btn2"forState:UIControlStateNormal];
[self.windowaddSubview:btn2];
【UIButton的类型】
1)UIButtonTypeRoundedRect - 圆角按钮
2)UIButtonTypeDetailDisclosure - 详情按钮
3)UIButtonTypeContactAdd - 加号按钮
4)UIButtonTypeInfoDark - 白色信息按钮
5)UIButtonTypeInfoLight - 灰色信息按钮
6)UIButtonTypeCustom - 自定义按钮
【UIButton的常用属性】
1)frame - 坐标
2)tag - 标签(用来给控件添加唯一标识)
3)adjustsImageWhenHighlighted - 高亮状态下按钮是否变暗
4)adjustsImageWhenDisabled - 禁用状态下按钮是否变暗
5)showsTouchWhen - 按钮高亮状态下是否发光
【UIButton的常用方法】
1)// 设置按钮上的文字
-(void)setTitle:(NSString *)title forState:(UIcontrolState)state;
[btn1 setTitle:@"btn1"forState:UIControlStateNormal];
2)// 设置按钮上标题文字的颜色
-(void)setTitleColor:(UIColor *)color forState:(UIControlState)state;
[btn1 setTitleColor:[UIColorblackColor]forState:UIControlStateNormal];
3)// 设置按钮上标题文字的阴影
-(void)setTitleShandowColor:(UIColor *)color forState:(UIControlState)state;
[btn1 setTitleShadowColor:[UIColorblueColor]forState:UIControlStateNormal];
[btn1.titleLabelsetShadowOffset:CGSizeMake(0, -1)];
4)// 设置按钮上的图片(不会被拉伸)
-(void)setImage:(UIImage *)image forState:(UIControlState)state;
[btn1 setImage:[UIImageimageNamed:@"ios/p.png"]forState:UIControlStateNormal];
5)// 设置按钮背景图(会拉伸充满整个btn)
-(void)setBackgroundImage:(UIImage *)imageforState:(UIControlState)state;
[btn1 setBackgroundImage:[UIImageimageNamed:@"ios/p.png"]forState:UIControlStateNormal];
6)// 设置按钮高亮状态下的颜色
-(void)setTintColor:(UIColor *)color;
[btn1 setTintColor:[UIColorwhiteColor]];
【UIButton的事件】
1)-(void)addTarget:(id)targetaction:(SEL)actionforControlEvents:(UIControlEvents)controlEvents
2)-(void)removeTarget:(id)targetaction:(SEL)actionforControlEvents:(UIControlEvents)controlEvents