ios--按钮控件

这篇博客介绍了iOS中UIButton的用途,详细讲解了它的常用属性,包括正常、高亮、禁用和选中四种状态,并提供了创建及设置按钮属性的示例代码。

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

一、UIButton

        作用:响应用户的点击事件

        常用属性


二、按钮的状态

     UIButton的状态

     UIControlStateNormal                       //正常状态

     UIControlStateHighlighted               //高亮状态

     UIControlStateDisabled                   //禁用状态

     UIControlStateSelected                   //选中状态

     UIControlStateApplication

     UIControlStateReserved

三、示例代码


创建UIButton并设置其属性

<pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc">    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.backgroundColor = [UIColor purpleColor];
    //工厂方法
    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];<span style="font-family: Arial, Helvetica, sans-serif;">    </span>
//禁用按钮
button.enabled = NO;

//按钮选中

button.selected = YES; //设置标题,状态正常 [button setTitle:@"normal" forState:UIControlStateNormal]; //设置标题,状态高亮 [button setTitle:@"highligh" forState:UIControlStateHighlighted]; //设置标题,状态禁用 [button setTitle:@"disable" forState:UIControlStateDisabled]; //设置标题,状态选中
 [button setTitle:@"select" forState:UIControlStateSelected];

    <span style="font-family: Arial, Helvetica, sans-serif;">//设置title的颜色</span>
    [button setTitleColor:[UIColor redColor] forState:<span style="font-family: Arial, Helvetica, sans-serif;">forState:UIControlStateNormal</span>]
    //设置frame,与按钮的类型
    button.frame = CGRectMake(90, 100, 140, 40);
    [self.window addSubview:button];
    [self.window makeKeyAndVisible];
    return YES;
    //设置背景图片
    [button setBackgroundImage:[UIImage imageNamed:@"love_normal"] forState:UIControlStateNormal];
    [button setBackgroundImage:[UIImage imageNamed:@"love_normal"] forState:UIControlStateSelected];
    //设置图片
    [button setImage:[UIImage imageNamed:@"love_normal"] forState:UIControlStateNormal];
    [button setImage:[UIImage imageNamed:@"love_normal"] forState:UIControlStateSelected];


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值