添加按钮(swift)

前端之家收集整理的这篇文章主要介绍了添加按钮(swift)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

添加按钮(swift)

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

猜你在找的Swift相关文章