UIButton 是 iOS 开发中的最常用的一个控件,那就是按钮。在开发过程中,页面上可以进行点击的事件基本上都是按钮,可以有各种个样的事件。下面就让我们来介绍一下 UIButotn 吧
一、UIButon 的创建和初始化
1、UIButton 的创建
用这种方式去创建一个 UIButton
@property (nonatomic, strong) UIButton *button;
2、UIButton 的初始化
使用这样取初始化 有多种 UIButtonType 类型 最常用到的是 UIButtonTypeCustom 自定义的 button
UIButtonType
UIButtonTypeCustom 此属性表明,该按钮的外观行为主要依靠开发者的设置 - 最为常用 -
UIButtonTypeSystem 系统默认
UIButtonTypeDetailDisclosure 用于显示当前列表项的详情
UIButtonTypeInfoLight 该按钮用于显示简短的说明
UIButtonTypeInfoDark 该按钮用于显示简短的说明
UIButtonTypeContactAdd 常用于添加联系人
UIButtonTypeRoundedRect 圆角矩形的按钮
self.button = [UIButton buttonWithType:UIButtonTypeCustom]; // 初始化Btn
二、UIButotn 的属性
在这里先补充一下按钮的一些情况(前五个最常用)
UIControlState 用来表示按钮在状态
UIControlStateNormal 普通状态下
UIControlStateHighlighted 高亮状态(即你按着按钮的时候
UIControlStateDisabled 不可点击状态 (即点击无效
UIControlStateSelected 选中状态
UIControlStateApplication 当应用程序标识使用时
UIControlStateReserved 保留状态
1、frame
用来设置位置和按钮大小
self.button.frame = CGRectMake(50, 100, self.view.bounds.size.width - 100, self.view.bounds.size.height - 200);
2、backgroundColor
设置按钮的背景颜色
self.button.backgroundColor = [UIColor whiteColor];
3、setTitle:forState:
设置按钮显示文字(参数一:文字,参数二:按钮状态)
[self.button setTitle:@"Button" forState:UIControlStateNormal];
4、setTitleColor:forState:
设置按钮文字颜色 (参数一:颜色 ,参数二:按钮状态)
[self.button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
5、setImage:forState:
设置按钮图片(参数一:图片,参数二:按钮状态)
[self.button setImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
6、setBackgroundImage:forState:
设置按钮背景图片 (参数一:图片,参数二:按钮状态)
[self.button setBackgroundImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
7、setTitleShadowColor:forState:
设置按钮文字阴影颜色(参数一:颜色,参数二:按钮状态)
[self.button setTitleShadowColor:[UIColor blackColor] forState:UIControlStateNormal];
8、setImageEdgeInsets:
设置图片的偏移(一般在图片文字一起设的时候使用)参数为 UIEdgeInsetsMake(left,top,right,bottom);某个方向偏移多少
//我这里不设置偏移
[self.button setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
9、setTitleEdgeInsets:
设置文字的偏移(一般在图片文字一起设的时候使用)
[self.button setTitleEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
10、titleLabel.font
设置文本字体的大小 只有这样做
self.button.titleLabel.font = [UIFont systemFontOfSize:15];
11、enabled
设置按钮能否点击 YES:表示可以 NO:不可点击
self.button.enabled = YES;
12、selected
设置按钮的选中状态 YES:选中 NO:未选中
self.button.selected = YES;
13、addTarget:action:forControlEvents:
添加按钮事件 (参数一 添加事件 对象,参数二:事件方法名,参数三:事件类型)
UIControlEvents
UIControlEventTouchDown 刚刚按下按钮
UIControlEventTouchDownRepeat 重复地按下的事件 这个事件的 tap 数量大于 1
UIControlEventTouchDragInside 手指在 control 的 bounds 范围内拖动的的事件
UIControlEventTouchDragOutside 当手指拖动刚好在 control 的 bounds 范围外的事件
UIControlEventTouchDragEnter 当手指拖动进入 control 范围内的事件
UIControlEventTouchDragExit 当手指从 control 范围内到它的 bounds 外的时候的事件
UIControlEventTouchUpInside 手指在在 control 内部 触发的点击事件 (最常用)
UIControlEventTouchUpOutside 手指在 control 外部 触发点击事件
UIControlEventTouchCancel 取消 control 当前触摸的事件
UIControlEventValueChanged 显示一系列不同的值 在 UISlider 中较为常见
先介绍这么一些 许多都是没有用到过的 许多控件都可以添加事件,是公用的
[self.button addTarget:self action:@selector(handleBtnAction:) forControlEvents:UIControlEventTouchUpInside];
14、hidden
按钮是否隐藏-顾名思义就是按钮是否可见
self.button.hidden = YES;
15、currentTitle
按钮当前文字
//打印按钮当前文字
NSLog(@"%@",self.button.currentTitle);
16、currentTitleColor
按钮当前文字颜色
//打印按钮当前文字颜色
NSLog(@"%@",self.button.currentTitleColor);
17、currentImage
按钮当前图片
NSLog(@"%@",self.button.currentImage);
18、currentBackgroundImage
按钮当前背景图片
NSLog(@"%@",self.button.currentBackgroundImage);
19、currentTitleShadowColor
按钮当前文字阴影颜色
NSLog(@"%@",self.button.currentTitleShadowColor);
20、contentVerticalAlignment
按钮垂直放置方式
self.button.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
21、contentHorizontalAlignment
按钮水平放置方式
self.button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
22、titleForState:
根据状态获取按钮文字
[self.button titleForState:UIControlStateNormal];
23、titleColorForState:
根据状态获取按钮文字颜色
[self.button titleColorForState:UIControlStateNormal];
24、titleShadowColorForState:
根据状态获取按钮文字阴影颜色
[self.button titleShadowColorForState:UIControlStateNormal];
25、imageForState:
根据状态获取按钮前景图片
[self.button imageForState:UIControlStateNormal];
26、backgroundImageForState:
根据状态获取按钮背景图片
[self.button backgroundImageForState:UIControlStateNormal];
27、attributedTitleForState:
根据状态获取按钮文字属性
[self.button attributedTitleForState:UIControlStateNormal];
28、setTintColor:
该属性与父视图有关,根据父视图来改变边框颜色(可能有误 ---- TODO)好在这个一直还没用到过
[self.button setTintColor:[UIColor clearColor]];
三、这里就是简单的按钮点击方法实现
在这里可以加入你想要实现的方式,例如 跳转界面,或者内部 Label 的值的改变,又或者等等等等,根据需求来定
- (void)handleBtnAction:(UIButton *)sender{
NSLog(@"你点击了按钮!!");
}
四、UIButton 小结
button 作为一个按钮,是开发中最常用的控件,例如你在 app 中的一个页面中可以点击的事件,几乎都是用按钮来实现的。用到触碰手势的概率很低,所以按钮还是很重要的。按钮简单易懂,难点在于‘ 图片文字偏移 ’ 后期可以自己封装一个图片在上文字在下的按钮方法,可以加快以后的开发哦~
好了,按钮的大概介绍到这里也就结束了!基本上的都已经写到了,可能还有不足
感谢大家的观看! 如果有错误,请帮忙指出!谢谢!!!