标签:
class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() // 1. 自定义UIbutton类型为Custom var btn : UIButton = UIButton.buttonWithType(UIButtonType.Custom) as UIButton // 2. 设置UIButton的尺寸 btn.frame = CGRectMake(100, 50, 100, 30); // 3. 设置普通状态下的button的字 btn.setTitle("swiftButton", forState: UIControlState.Normal) // 4. 设置普通状态下的buton字的颜色 btn.setTitleColor(UIColor.redColor(), forState: UIControlState.Normal) // 5. 设置高亮状态下的button的字的 btn.setTitle("HightLight", forState: UIControlState.Highlighted) // 6. 设置高亮状态下的button的字的颜色 btn.setTitleColor(UIColor.blueColor(), forState: UIControlState.Highlighted) // 7. 加载图片 ! 为可选类型,说明返回的图片一定是存在的不存在将崩溃 var btnImageN: UIImage = UIImage(named: "1")! var btnImageHL: UIImage = UIImage(named: "2")! // 8. 设置button的图片 //btn.setImage(btnImageN, forState: UIControlState.Normal) // 9. 设置button的图片 //btn.setImage(btnImageHL, forState: UIControlState.Normal) // 10. 设置普通状态下button的背景图片 btn.setBackgroundImage(btnImageN, forState: UIControlState.Normal) // 11. 设置高亮状态下button的背景图片 btn.setBackgroundImage(btnImageHL, forState: UIControlState.Highlighted) // 12. 设置UIButton的触发事件 btn.addTarget(self, action: "btnClick:", forControlEvents: UIControlEvents.TouchUpInside) self.view.addSubview(btn) } func btnClick(btn:UIButton!) { println("btnClick") } }
标签:
原文地址:http://www.cnblogs.com/advacedprogrammer/p/4294913.html