前端之家收集整理的这篇文章主要介绍了
添加按钮(swift),
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
添加按钮(swift)
@H_
301_7@// Button
class MyButton: UIButton {
var onClick: () -> () = { _
in () }
@objc func tapped(sender: AnyObject) {
onClick()
}
}
func button(text: String,onClick: () -> ()) -> UIButton {
let b = MyButton(type:
.Custom)
b
.userInteractionEnabled = true
b
.frame = CGRect(
x:
0,
y: buttonOffset,width:
200,height:
30)
b
.backgroundColor = UIColor
.grayColor()
b
.setTitle(text,forState:
.Normal)
b
.addTarget(b,action:
"tapped:",forControlEvents: UIControlEvents
.TouchUpInside)
b
.onClick = onClick
return b
}
@H_
301_7@self.view.addSubview
(button("Add health point",onClick: { () -> () in print("click") }))