IOS开发系列之Swift_UI_Btn
import UIKit
class ViewController: UIViewController {
//声明一个btn
var exampleBtn : UIButton!
override func viewDidLoad() {
super.viewDidLoad()
//声明一个方法
makeBtn()
// Do any additional setup after loading the view, typically from a nib.
}
//方法
private func makeBtn() {
//初始化
self.exampleBtn = UIButton.init()
//设置frame
self.exampleBtn.frame = CGRectMake(100, 100, 60, 60)
//设置背景颜色
//self.exampleBtn.backgroundColor = UIColor.lightGrayColor()
//设置btn 字体大小
self.exampleBtn.titleLabel?.font = UIFont.systemFontOfSize(12)
//设置btn 的文字
self.exampleBtn.setTitle("clickBnt", forState: UIControlState.Normal)
//标记tag
self.exampleBtn.tag = 520
//添加点击事件
self.exampleBtn.addTarget(self, action: "clickBtnDown:", forControlEvents: UIControlEvents.TouchUpInside)
//设置btn的图片
self.exampleBtn.setImage(UIImage(imageLiteral: "103"), forState: UIControlState.Normal)
self.view .addSubview(self.exampleBtn)
}
//btn 的点击事件
func clickBtnDown(btn : UIButton) {
print("\(btn.tag)")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}