【iOS 开发】基础控件:UIButton

iOS 开发之基础控件 UIButton

1. UIButton 常规使用


let uiButton = UIButton(type: UIButton.ButtonType.system)
// UIButton 设置位置
uiButton.frame = CGRect(x: 10, y: 230, width: 360, height: 38)
// UIButton 设置颜色
uiButton.backgroundColor = UIColor.cyan
// UIButton 设置标题
uiButton.setTitle("UIButton title", for: .normal)
// UIButton 设置标题文字颜色
uiButton.setTitleColor(UIColor.black, for: .normal)

// UIButton 添加点击事件
uiButton.addTarget(self, action: #selector(touchUpInside), for: .touchUpInside)
// 添加 UIButton 到视图上
self.view.addSubview(uiButton)


@objc func touchUpInside() {
    print("按钮被按下,并且在控件区域内抬起")
}


效果展示:
button_normal


2. UIButton 设置内容图片


let uiButton2 = UIButton(type: UIButton.ButtonType.custom)
uiButton2.frame = CGRect(x: 10, y: 280, width: 360, height: 80)
uiButton2.backgroundColor = UIColor.cyan
uiButton2.setTitle("Content Image", for: .normal)
uiButton2.setTitleColor(UIColor.black, for: .normal)
// UIButton 设置内容图片
uiButton2.setImage(UIImage(named: "demo"), for: .normal)
self.view.addSubview(uiButton2)


效果展示:
button_content


3. UIButton 设置背景图片

let uiButton3 = UIButton(type: UIButton.ButtonType.custom)
uiButton3.frame = CGRect(x: 10, y: 370, width: 360, height: 80)
uiButton3.backgroundColor = UIColor.cyan
uiButton3.setTitle("Background Image", for: .normal)
uiButton3.setTitleColor(UIColor.black, for: .normal)
// UIButton 设置背景图片
uiButton3.setBackgroundImage(UIImage(named: "demo"), for: .normal)
self.view.addSubview(uiButton3)

效果展示:
button_background


附 Github 源码:

ViewController.swift

  • 9
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值