一.UIButton实例化
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
/*
1.UIButtonTypeSystem
2.UIButtonTypeRoundedRect 圆角按钮, iOS7之前有效,iOS7之后无效果 和UIButtonTypeSystem效果一样
3.UIButtonTypeInfoLight
4.UIbuttonTypeInfoDark
5.UIButtonTypeDetailDisclosure 3-5效果一样 按钮中有蓝色圆圈i
6.UIbuttonTypeContactAdd 蓝色圆圈+
*/
二.UIButton属性相关
1.背景颜色
button.backgroundColor = [UIColor grayColor];
2.位置及大小
button.frame = CGRectMake(20,100,280,40);
3.按钮是否可用
默认为YES , NO为不可用
button.enable = YES;
4.设置文字: setTitle:forState:
/*
UIControlStateNormal
一般状态
UIControlStateHighlighted
高亮状态
UIControlStateDisabled 不可用状态
*/
[button setTitle:@"button" forState:UIControlStateNormal];
5.设置文字颜色
[button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
6.这是文字的大小
button.title.font = [UIFont systemFontOfSize:24.0];
7.按钮添加点击事件 按钮的点击事件必须实现
[button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
/*
1.Target: 按钮点击之后通知给谁(通知给奔雷):self;
2.action:方法 时间 @selector(方法名字)
3.forControlEvents: 按钮要如何点击,点击方式
*/
点击方式:
1.UIControlEventTouchUpInside
里进里除
2.UIControlEventTouchUpOutside
里进外出
3.UIControlEventTouchDragOutside
里进外拖拽
4.UIControlEventTouchDragInside
里进里拖拽
5.UIControlEventTouchDragExit
拖拽出去
6.UIControlEventTouchDragEnter
拖拽出去再返回
7.UIControlEventTouchDownRepeat
双击
8.UiControlEventTouchDown
点中就有操作
点击方法的实现
不带参数的方法实现
- (void)buttonClick{
}
带参数的方法实现
- (void)buttonClick:(UIButton *)button{
}
8.获取按钮的文字
button.currentTitle 返回NSString
当按钮带有高亮状态,找到高亮状态的文字;无高亮状态,找到正常状态的文字
button.titleLabel.text 返回NSString
当高亮状态点击出现时,找到高亮状态的文字;当高亮状态未出现时,找到正常状态的文字,无高亮状态,找到正常状态的文字
9.图片类型按钮 初始化时选择UIButtonTypeCustom
设置按钮背景图片
UIImage *image = [UIImage imageNamed:@"xxx.png"];
[button setImage:image forState:UIControlStateNormal];
当按钮大小小于等于图片大小时,图片压缩或正常显示;当按钮大小大于图片大小时,图片会被拉伸,按按钮大小显示
三.UIImageView
1.实例化
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(100,100,100,100)];
2.属性
背景颜色
imageView.backgroundColor = [UIColor grayColor];
图片属性
imageView.image = [UIImage imageNamed:@"xxx.png"];