博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
IOS开发实战:UIButton按钮控件
阅读量:2222 次
发布时间:2019-05-08

本文共 5844 字,大约阅读时间需要 19 分钟。

一,创建UIButton按钮控件项目

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之间的浮点值)

运行工程,当点击按钮时,随机切换当前界面背景颜色:

点击按钮改变背景颜色

以上按钮控件未未加入任何修饰

二,为UIButton添加背景和内容图片

点击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];}
你可能感兴趣的文章
数据类型 java转换
查看>>
"NetworkError: 400 Bad Request - http://172.16.47.117:8088/rhip/**/####t/approval?date=976
查看>>
mybatis 根据 数据库表 自动生成 实体
查看>>
C结构体、C++结构体、C++类的区别
查看>>
进程和线程的概念、区别和联系
查看>>
CMake 入门实战
查看>>
绑定CPU逻辑核心的利器——taskset
查看>>
Linux下perf性能测试火焰图只显示函数地址不显示函数名的问题
查看>>
c结构体、c++结构体和c++类的区别以及错误纠正
查看>>
Linux下查看根目录各文件内存占用情况
查看>>
A星算法详解(个人认为最详细,最通俗易懂的一个版本)
查看>>
利用栈实现DFS
查看>>
(PAT 1019) General Palindromic Number (进制转换)
查看>>
(PAT 1073) Scientific Notation (字符串模拟题)
查看>>
(PAT 1080) Graduate Admission (排序)
查看>>
Play on Words UVA - 10129 (欧拉路径)
查看>>
mininet+floodlight搭建sdn环境并创建简答topo
查看>>
【linux】nohup和&的作用
查看>>
Set、WeakSet、Map以及WeakMap结构基本知识点
查看>>
【NLP学习笔记】(一)Gensim基本使用方法
查看>>