本文共 5844 字,大约阅读时间需要 19 分钟。
1,打开Xcode,创建名为UIButtonTest的工程
如何创建工程:2,在ViewController.m的viewDidLoad方法中添加如下代码:
- (void)viewDidLoad { [super viewDidLoad]; UIButton * button = [UIButton buttonWithType:UIButtonTypeSystem]; button.frame=CGRectMake(40, 100, 240, 30); button.backgroundColor=[UIColor redColor]; [button setTitle:@"点我一下" forState:UIControlStateNormal]; [button addTarget:self action:@selector(changeColor) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:button];}// 按钮点击响应事件-(void)changeColor{ self.view.backgroundColor=[UIColor colorWithRed:arc4random()%255/255.0 green:arc4random()%255/255.0 blue:arc4random()%255/255.0 alpha:1];}
通过UIButton类中buttonWithType方法进行按钮控件的初始化需要传入UIButtonType类型参数确定按钮风格,枚举如下:UIButtonTypeCustom // 自定义类型UIButtonTypeSystem // 标准的系统类型UIButtonTypeDetailDisclosure // 详情按钮类型UIButtonTypeContactAdd // 添加按钮类型
UIButtonTypeCustom 自定义类型:
custom风格的背景色,字体颜色,字体,点击状态属性全部采用默认值,需开发者自行设置
UIButtonTypeSystem 标准的系统类型
System风格是系统定义好的一组属性设置的风格
UIButtonTypeDetailDisclosure 详情按钮类型
DetailDisclosure风格会在左边显示一个详情小图标
UIButtonTypeContactAdd 添加按钮类型
ContactAdd风格则是在按钮左边显示一个添加小图标
继续分析代码:
setTitle方法: 参数1:标题文字 参数2:显示此标题文字时的按钮状态 UIControlState定义了一些用于交互控件的交互状态 UIControlStateNormal = 0 // 正常状态 UIControlStateHightLighted = 1 << 0 // 高亮状态 UIControlStateDisabled =1 << 1 // 不可用状态 UIControlStateSelected = 1 << 2 // 选中状态 Normal状态是按钮的初始状态,此时无任何交互 HightLighted为高亮状态,即用户点击按钮但并未抬起时 Disabled为不可用状态,此时用户点击操作无效 Selected为选中状态,用于切换开关等按钮addTarget方法: 添加触发响应事件 参数1:执行触发方法的对象,一般为self,即当前对象本身 参数2:触发事件名称 参数3:方法触发条件,需传入UIControlEvents数据类型 UIControlEvents选项决定了触发交互的用户操作行为: UIControlEventTouchDown, // 按下时触发 UIControlEventTouchDownRepeat,// 多次重复按下时触发 UIControlEventTouchDragInside,// 在控件范围内拖滑移动时触发 UIControlEventTouchDragOutside,// 在控件范围内按下并拖滑到控件范围外时触发 UIControlEventTouchDragEnter, // 拖动进控件范围后触发 UIControlEventTouchDragExit, // 拖动结束时触发 UIControlEventTouchUpInside, // 在控件范围内按下并在范围内抬起,即单击 UIControlEventTouchUpOutside, // 在控件范围内按下并在范围外抬起 UIControlEventTouchCancel, // 触摸事件被取消时触发 UIControlEventValueChanged, // 控件Value值改变时触发
changeColor方法:
// 按钮点击响应事件-(void)changeColor{ self.view.backgroundColor=[UIColor colorWithRed:arc4random()%255/255.0 green:arc4random()%255/255.0 blue:arc4random()%255/255.0 alpha:1];}
changeColor方法使用通过RGBA方式创建颜色对象4个参数分别设置:红,绿,蓝3色值+透明度(每个参数都取0~1之间的浮点值)
运行工程,当点击按钮时,随机切换当前界面背景颜色:
以上按钮控件未未加入任何修饰
点击Assets.xcassets包文件,选择图片拖入素材区:
为按钮设置背景图片:
UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];[button setBackgroundImage:[UIImage imageNamed:@"image"] forState:UIControlStateNormal];
运行代码:
可以发现,当图片作为背景时,按钮标题在显示图片层之上
设置图片为内容图片,图片和标题并列显示
UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];[button setImage:[UIImage imageNamed:@"image2"] forState:UIControlStateNormal];
图片和文字左右并排且共同居中显示
UIButton中图片和文字显示的更多排版
开发者可能会需要上下排列,或以其他方式进行图片和文字的排列UIButton中提供了接口供开发者对内容,图片和文字的位置设置[button setContentEdgeInsets:UIEdgeInsetsMake(0,0,0,0)][button setImageEdgeInsets:UIEdgeInsetsMake(0,0,0,0)][button setTitleEdgeInsets:UIEdgeInsetsMake(0,0,0,0)]setContentEdgeInsets方法用于设置整体内容的区域偏移量setImageEdgeInsets方法只设置内容图片的位置偏移量setTitleEdgeInsets方法只设置内容标题的位置偏移量UIEdgeInsetsMake方法4个参数分别代表上,下,左,右4个方向的偏移量
- (void)viewDidLoad { [super viewDidLoad];// 通过UIButton类中buttonWithType方法进行按钮控件的初始化// 需要传入UIButtonType类型参数确定按钮风格,枚举如下:// UIButtonTypeCustom // 自定义类型// UIButtonTypeSystem // 标准的系统类型// UIButtonTypeDetailDisclosure // 详情按钮类型// UIButtonTypeContactAdd // 添加按钮类型 UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom]; button.frame = CGRectMake(40, 100, 240, 30); [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];// UIControlState定义了一些用于交互控件的交互状态// UIControlStateNormal = 0 // 正常状态// UIControlStateHightLighted = 1 << 0 // 高亮状态// UIControlStateDisabled =1 << 1 // 不可用状态// UIControlStateSelected = 1 << 2 // 选中状态//// Normal状态是按钮的初始状态,此时无任何交互// HightLighted为高亮状态,即用户点击按钮但并未抬起时// Disabled为不可用状态,此时用户点击操作无效// Selected为选中状态,用于切换开关等按钮 [button setTitle:@"点我一下" forState:UIControlStateNormal];// UIControlEvents选项决定了触发交互的用户操作行为:// UIControlEventTouchDown, // 按下时触发// UIControlEventTouchDownRepeat,// 多次重复按下时触发// UIControlEventTouchDragInside,// 在控件范围内拖滑移动时触发// UIControlEventTouchDragOutside,// 在控件范围内按下并拖滑到控件范围外时触发// UIControlEventTouchDragEnter, // 拖动进控件范围后触发// UIControlEventTouchDragExit, // 拖动结束时触发// UIControlEventTouchUpInside, // 在控件范围内按下并在范围内抬起,即单击// UIControlEventTouchUpOutside, // 在控件范围内按下并在范围外抬起// UIControlEventTouchCancel, // 触摸事件被取消时触发// UIControlEventValueChanged, // 控件Value值改变时触发 [button addTarget:self action:@selector(changeColor) forControlEvents:UIControlEventTouchUpInside];// // 当图片作为背景时,按钮标题在显示图片层之上// [button setBackgroundImage:[UIImage imageNamed:@"image"]// forState:UIControlStateNormal]; [button setImage:[UIImage imageNamed:@"image2"] forState:UIControlStateNormal]; [self.view addSubview:button];}// 按钮点击响应事件-(void)changeColor{ self.view.backgroundColor=[UIColor colorWithRed:arc4random()%255/255.0 green:arc4random()%255/255.0 blue:arc4random()%255/255.0 alpha:1];}