加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

添加按钮(swift)

发布时间:2020-12-14 01:28:59 所属栏目:百科 来源:网络整理
导读:添加按钮(swift) 1.定义button // Buttonclass MyButton: UIButton { var onClick: () - () = { _ in () } @objc func tapped(sender: AnyObject) { onClick() }}func button(text: String,onClick: () - ()) - UIButton { let b = MyButton(type: .Custom )

添加按钮(swift)

  • 1.定义button
// 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
}
  • 2.使用
self.view.addSubview(button("Add health point",onClick: { () -> () in print("click") }))

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读