这个控件是 Apple 默认 UIButton 的子类。它增加了一些功能,减少了自定义 UIButton 的外观和行为的代码行数,并利用了一些 Swift 的优点。
将控件集成到项目中的最简单方法是将 ALButton.swift 文件复制到您的项目中。
初始化按钮有三种方式
//- 1. Full Initialization
let style = ALButton.ButtonStyle()
//-- set style
let myButton = ALButton(title: "Button Title", style: style, touchUpInsideHandler: {
(sender ) in
//-- Implement touch up inside event here
})
//- 2. Simple initialization with style:
var style = ALButton.ButtonStyle()
//-- set style
let myButton = ALButton(title: "Button Title", style: style)
//- 3. Short initialization
let myButton = ALButton(title: "Button Title")
您还可以使用尾部闭包更方便地初始化按钮
//- Short Initialization
let myButton = ALButton(title: "Button Title") {
sender in
//-- Implement touch up inside event here
}
//- Full Initialization
let myButton = ALButton(title: "Button Title", style: style) {
sender in
//- Implement touch up inside code here
}
在初始化按钮时,您可以选择不添加 Touch Up Inside 处理程序。您可以使用以下代码在任何点添加处理程序
myButton.touchUpInside {
sender in
//-- Implement touch up inside code here
}
要为按钮应用样式,您应该使用 ButtonStyle 结构。通过它,您可以设置以下属性:titleFont
、titleColor
、disabledTitleColor
、backgroundColor
、highlightColor
、disabledColor
。
您可以选择在初始化时或之后通过设置按钮的 style
属性来传递此样式。
此组件在 MIT 许可证下可用