Swift之贪婪的UIButton

一、内容概要

这里写图片描述
按钮是所有UI体系中非常重要的组件,在iOS中按钮UIButton的使用也非常灵活,本文将从以下几点介绍UIButton的使用(基于Swift2.0):

1.UIButton基础
2.UIButton图片使用
3.圆角按钮
4.复选框按钮
5.倒计时按钮(闪烁问题也轻松解决)
6.贪婪按钮(父控件事件也归我,扩大事件响应区域)

二、UIButton基础

2.1创建

UIButton提供了一个简单的构造方法

convenience init(type buttonType: UIButtonType)

此方法中需要我们传入一个UIButtonType枚举类型,使用代码如下:

    func createButton() {
        let button = UIButton(type: UIButtonType.System)
        button.frame = CGRectMake(50, 50, 100, 50)
        button.setTitle("确定", forState: UIControlState.Normal)
        button.addTarget(self, action: "buttonPressed:", forControlEvents: UIControlEvents.TouchUpInside)
        self.view.addSubview(button)
    }

    func buttonPressed(button: UIButton) {

    }
Tips:

1.设置按钮标题时,一定要通过

    func setTitleColor(color: UIColor?, forState state: UIControlState)

不可通过

    button.titleLabel?.text = "确定"

此方式会在点击时标题自动变为setTitleColor方法Normal状态下的文字

2.2图片使用

UIButton提供了以下两个接口使用图片:

    func setImage(image: UIImage?, forState state: UIControlState)
    func setBa
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值