1.初始化

        /**
UIButtonType.system:前面不带图标,默认文字颜色为蓝色,有触摸时的高亮效果
UIButtonType.custom:定制按钮,前面不带图标,默认文字颜色为白色,无触摸时的高亮效果
UIButtonType.contactAdd:前面带“+”图标按钮,默认文字颜色为蓝色,有触摸时的高亮效果
UIButtonType.detailDisclosure:前面带“!”图标按钮,默认文字颜色为蓝色,有触摸时的高亮效果
UIButtonType.infoDark:为感叹号“!”圆形按钮
UIButtonType.infoLight:为感叹号“!”圆形按钮
(注意:自ios7起,infoDark、infoLight、detailDisclosure效果都是一样的)
*/
let buttonOne = UIButton.init(type: UIButtonType.custom)

2. 设置

        /**
normal
highlighted
disabled
selected
*/
buttonOne.setTitle("这个是一个button", for: UIControlState.normal)
buttonOne.setImage(UIImage.init(named: "1.jpg"), for: UIControlState.normal) // 设置背景颜色
buttonOne.setBackgroundImage(UIImage.init(named: ""), for: UIControlState.normal) // 点击事件
buttonOne.addTarget(self, action: #selector(buttonOneClicked), for: UIControlEvents.touchUpInside)

3.代码

import UIKit

let kScreenWidth = UIScreen.main.bounds.size.width

class ViewController: UIViewController {

    override func viewDidLoad() {
super.viewDidLoad() self.addButtonOne()
} func addButtonOne() { self.view.addSubview(buttonOne)
} func buttonOneClicked() {
print("点击了")
} // 懒加载一个UIButton
lazy var buttonOne: UIButton = { /**
UIButtonType.system:前面不带图标,默认文字颜色为蓝色,有触摸时的高亮效果
UIButtonType.custom:定制按钮,前面不带图标,默认文字颜色为白色,无触摸时的高亮效果
UIButtonType.contactAdd:前面带“+”图标按钮,默认文字颜色为蓝色,有触摸时的高亮效果
UIButtonType.detailDisclosure:前面带“!”图标按钮,默认文字颜色为蓝色,有触摸时的高亮效果
UIButtonType.infoDark:为感叹号“!”圆形按钮
UIButtonType.infoLight:为感叹号“!”圆形按钮
(注意:自ios7起,infoDark、infoLight、detailDisclosure效果都是一样的)
*/
let buttonOne = UIButton.init(type: UIButtonType.custom) buttonOne.frame = CGRect.init(x: , y: , width: kScreenWidth - , height: )
buttonOne.backgroundColor = UIColor.red /**
normal
highlighted
disabled
selected
*/
buttonOne.setTitle("这个是一个button", for: UIControlState.normal)
buttonOne.setImage(UIImage.init(named: "1.jpg"), for: UIControlState.normal) // 设置背景颜色
buttonOne.setBackgroundImage(UIImage.init(named: ""), for: UIControlState.normal) // 点击事件
buttonOne.addTarget(self, action: #selector(buttonOneClicked), for: UIControlEvents.touchUpInside) return buttonOne
}()
}

Swift_UIButton的更多相关文章

随机推荐

  1. 洛谷 P3203 [HNOI2010]弹飞绵羊 解题报告

    P3203 [HNOI2010]弹飞绵羊 题目描述 某天,Lostmonkey发明了一种超级弹力装置,为了在他的绵羊朋友面前显摆,他邀请小绵羊一起玩个游戏.游戏一开始,Lostmonkey在地上沿着一 ...

  2. POJ 开关问题 解题报告

    开关问题 Time Limit: 1000MS Memory Limit: 30000K Description 有N个相同的开关,每个开关都与某些开关有着联系,每当你打开或者关闭某个开关的时候,其他 ...

  3. 【COGS 461】[网络流24题] 餐巾 最小费用最大流

    既然是最小费用最大流我们就用最大流来限制其一定能把每天跑满,那么把每个表示天的点向T连流量为其所需餐巾,费用为0的边,然后又与每天的餐巾对于买是无限制的因此从S向每个表示天的点连流量为INF,费用为一 ...

  4. Elasticsearch报错

    [2018-07-12T10:32:47,642][INFO ][o.e.b.BootstrapChecks ] [VfCcJIq] bound or publishing to a non-loop ...

  5. C#语法糖大汇总【转发】

    首先需要声明的是“语法糖”这个词绝非贬义词,它可以给我带来方便,是一种便捷的写法,编译器会帮我们做转换:而且可以提高开发编码的效率,在性能上也不会带来损失.这让java开发人员羡慕不已,呵呵. 1.  ...

  6. pycharm激活(JetBrains IDEA 系列产品通用xx方法(license server))

    http://xclient.info/a/f0b9738a-36fd-8a97-a966-0d3db497092d.html .打开激活窗口 .选择 Activate new license wit ...

  7. windows10-seaslog安装笔记

    1.seasLog在windows下的安装 首先,要下载seasLog的dll文件,下载地址:http://pecl.php.net/package/SeasLog  选择对应你的系统和php版本的d ...

  8. xcode 10 出现 :-1: Multiple commands produce '/Users/.../Library/Developer/Xcode/DerivedData/.../Build/Products/Dev-iphonesimulator/TLYShyNavBar/TLYShyNavBar.framework/Info.plist': 1) Target 'TLYShyNavB

    错误提示如下: :-1: Multiple commands produce '/Users/.../Library/Developer/Xcode/DerivedData/.../Build/Pro ...

  9. MAC电脑密码破解

    [第一个方法] 开机,启动时按cmd+S,进入Single User Mode,出现像DOS一样的提示符#root> 在#root>下输入(注意空格,大小写)   fsck -y moun ...

  10. WebComponents001

    Sample1: ShadowDom 隔离style,替换显示内容 <button>Hello, world!</button> <script> var host ...