UIButton 的 常用方法

本文详细介绍了 iOS 开发中 UIButton 的常见使用方法,包括初始化、设置样式及响应事件等。适合初学者快速上手 UIButton 的使用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Btton 的  常用方法


只是简单的 总结  一下 经常用的  Buttonde 使用方法 


初学者 看看还是挺好接受的

初始化

在使用MRC 的时候    最好  使用   构造器   因为此构造器无需考虑引用计数

UIButton *button = [UIButton buttonWithType:5];

因为buttonWithType  为  枚举类型   即可以写它的代号

也可以写它的名字   以下为它的成员

UIButtonTypeCustom = 0,                         // no button type

    UIButtonTypeSystem NS_ENUM_AVAILABLE_IOS(7_0),  // standard system button


    UIButtonTypeDetailDisclosure,

    UIButtonTypeInfoLight,

    UIButtonTypeInfoDark,

    UIButtonTypeContactAdd,

    

    UIButtonTypeRoundedRect = UIButtonTypeSystem,   // Deprecated, use UIButtonTypeSystem instead

设置



设置坐标

button.frame = CGRectMake(30, 30, 30, 30);

设置背景颜色

button.backgroundColor = [UIColor whiteColor];

设置button上的文字

state :枚举类型 表示button现在的状态

    UIControlStateNormal       = 0,

    UIControlStateHighlighted  = 1 << 0,                  // used when UIControl isHighlighted is set

    UIControlStateDisabled     = 1 << 1,

    UIControlStateSelected     = 1 << 2,                  // flag usable by app (see below)

    UIControlStateApplication  = 0x00FF0000,              // additional flags available for application use

    UIControlStateReserved     = 0xFF000000               // flags reserved for internal framework use


[button setTitle:@"按钮" forState:UIControlStateNormal];


设置button文字颜色

button文字默认颜色为蓝色

[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];


添加点击事件  就是给按钮绑定一个 方法

[button addTarget:(id) action:(SEL) forControlEvents:(UIControlEvents)];

参数1(id位置):执行方法的对象self

参数2(SEL位置):方法名称

参数3 (UIControlEvents) :button处于什么状态时候触发方法

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值