当前位置:网站首页>[OC]-<UI入门>--常用控件-UIButton
[OC]-<UI入门>--常用控件-UIButton
2022-07-06 08:50:00 【关于小司】
文章目录
UIButton
UIButton基础
- 按钮是所有UI体系中非常重要的组件,在iOS中按钮UIButton的使用也非常灵活
创建文字UIButton
- 对于UI对象 我们在创建的时候注意一些事项,创建UI对象的方法是类方法还是实例方法?
- 创建UI对象之后,需注意大小,位置 颜色 等细节
- 创建一个UIButton
UIButton* btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
只能通过类方法来创建buttonWithType
圆角类型:UIButtonTypeRoundedRect
设置UIButton 的属性
- 设置button按钮初始化位置
btn.frame = CGRectMake(100, 100, 100, 40);
- 设置按钮内容
参数1:字符串类型,显示按钮文字
参数2:设置文字状态类型:UIControlStateNormal 正常状态
[btn setTitle:@"按钮1" forState:UIControlStateNormal];
- 显示文字状态类型
IControlStateHighlighted 按下状态
[btn setTitle:@"按钮按下状态" forState:UIControlStateHighlighted]
- 背景颜色
btn.backgroundColor = [UIColor grayColor];
- 设置文字颜色
参数1 颜色
参数2 状态 UIControlStateNormal 正常状态
[btn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
- 按下状态颜色
- 参数1 -颜色
参数2 -状态 UIControlStateHighlighted
[btn setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
- 设置字体大小
btn.titleLabel.font = [UIFont systemFontOfSize:24];
添加到视图屏幕显示
[self.view addSubview:btn];
不带图片的UIButton 效果

带图片的UIButton
- 对于正常的应用程序 我们不可能是这样子的按钮吧,一般来说都是带图片的Button按钮赏心悦目 接下啦演示如何创建一个带图片的Button
- 整体代码
-(void) crateImageBtn {
//创建一个自定义类型button
UIButton* btnImage = [UIButton buttonWithType:UIButtonTypeCustom];
btnImage.frame = CGRectMake(100, 200, 100, 100);
UIImage* icon01 = [UIImage imageNamed:@"btn01.jpeg"];
UIImage* icon02 = [UIImage imageNamed:@"btn02.jpeg"];
//设置按钮图片的方法
//p1 显 示图片对象
//p2 控件状态
[btnImage setImage:icon01 forState:UIControlStateNormal];
[btnImage setImage:icon02 forState:UIControlStateHighlighted];
[self.view addSubview:btnImage];
}
带图片的Button创建注意事项及代码解读
- 我们先自定义一个Button
- 我们的按钮是要有两个状态的 按下和普通状态
UIControlStateNormal 普通
UIControlStateHighlighted 按下
- 两个状态对应的图片加载-图片名字加格式
UIImage* icon01 = [UIImage imageNamed:@"btn01.jpeg"]; UIImage* icon02 = [UIImage imageNamed:@"btn02.jpeg"];
- 设置按钮图片的方法
- 参数一 : 显示图片对象
- 参数二: 控件状态
[btnImage setImage:icon01 forState:UIControlStateNormal];
[btnImage setImage:icon02 forState:UIControlStateHighlighted];
效果
- 普通状态

- 按下状态

UIButton事件处理
- 所谓事件处理就是在按下按钮的时候我们添加我们要用的事件当用户点击按钮的时候会发生按钮所对应的事件 从而达到我们要的功能实现
- 向按钮添加事件函数—一个按钮可以添加多个事件函数
事件处理的多个参数
-事件处理函数
btn addTarget:(nullable id) action:(nonnull SEL) forControlEvents:(UIControlEvents)
先明白参数的意义
1)
p1:谁来实现事件函数,实现的对象就是谁a、
addTarget:self 实现者就是自己
p2:@selector(pressBtn):函数对象,当按钮满足p3事件类型的时候,调用该函数
p3:UIControlEvent:事件处理函数类型
UIControlEventTouchUpOutside:当手指离开屏幕时并且手指的位置在按钮范围之外就会触发事件函数
UIControlEventTouchDown:当我们的手指只要触碰屏幕上时
实现事件
-(void) createButton {
//创建圆角button
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(100, 100, 80, 40);
[btn setTitle:@"按钮1" forState:UIControlStateNormal];
[btn addTarget:self action:@selector(pressBtn:) forControlEvents:UIControlEventTouchUpInside];
//UIControlEventTouchUpInside:当手指离开屏幕时并且手指的位置在按钮范围内就会触发事件函数
//2)触碰事件函数
[btn addTarget:self action:@selector(touchDown) forControlEvents:UIControlEventTouchDown];
[self.view addSubview:btn];
//同一个函数可以被不同按钮调用
UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn2.frame = CGRectMake(100, 100, 40, 80);
[btn2 setTitle:@"按钮2" forState:UIControlStateNormal];
[btn2 addTarget:self action:@selector(pressBtn:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn2];
//设置按钮标记--不知道按钮的指针才这样写
btn.tag = 101;
btn2.tag = 102;
事件需要调用的函数(参数之一)
//参数是调用此函数按钮本身--推荐这样写
//好处如下
-(void) pressBtn:(UIButton*) btn {
if (btn.tag == 101) {
NSLog(@"btn1 pressed");
} else {
NSLog(@"btn2 pressed");
}
// NSLog(@"btn pressed");
效果

- 我们先点击按钮一 然后按钮被触碰–打印
- 点击按钮二 然后也被触碰–打印
边栏推荐
- LeetCode:剑指 Offer 48. 最长不含重复字符的子字符串
- Warning in install. packages : package ‘RGtk2’ is not available for this version of R
- Export IEEE document format using latex
- Deep anatomy of C language -- C language keywords
- 生成器参数传入参数
- MYSQL卸载方法与安装方法
- 704 binary search
- Navicat Premium 创建MySql 创建存储过程
- Compétences en mémoire des graphiques UML
- The problem and possible causes of the robot's instantaneous return to the origin of the world coordinate during rviz simulation
猜你喜欢

Compétences en mémoire des graphiques UML

704 binary search

JVM quick start

如何正确截取字符串(例:应用报错信息截取入库操作)

Chapter 1 :Application of Artificial intelligence in Drug Design:Opportunity and Challenges

The harm of game unpacking and the importance of resource encryption

项目连接数据库遇到的问题及解决

企微服务商平台收费接口对接教程

Deep analysis of C language pointer

广州推进儿童友好城市建设,将探索学校周边200米设安全区域
随机推荐
Bitwise logical operator
swagger设置字段required必填
View computer devices in LAN
Detailed explanation of heap sorting
Visual implementation and inspection of visdom
Problems encountered in connecting the database of the project and their solutions
Sublime text using ctrl+b to run another program without closing other runs
POI add write excel file
How to effectively conduct automated testing?
LeetCode:673. Number of longest increasing subsequences
【嵌入式】使用JLINK RTT打印log
Navicat Premium 创建MySql 创建存储过程
LeetCode:236. 二叉树的最近公共祖先
TCP/IP协议
Excellent software testers have these abilities
UML圖記憶技巧
Crash problem of Chrome browser
Fairguard game reinforcement: under the upsurge of game going to sea, game security is facing new challenges
Tdengine biweekly selection of community issues | phase III
TP-LINK 企业路由器 PPTP 配置