UIButton的创建 注意点 事件监听

2017-04-23  本文已影响43人  壹点微尘
import UIKit


class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        //UIButton()括号内什么也不写,是custom模式
    let btn = UIButton()
        
        btn.setTitle("点我啊", for: .normal)
        btn.backgroundColor = #colorLiteral(red: 0.300648272, green: 1, blue: 1, alpha: 1)
//        btn.setImage(#imageLiteral(resourceName: "headerImage"), for: .normal)
        btn.sizeToFit()
        //btn.sizeToFit()一定要写在btn.center = view.center的前面
        btn.center = view.center
        btn.addTarget(self, action: #selector(btnClick), for: .touchUpInside) //#selector(btnClick)不用传btn
        view.addSubview(btn)

        
    }

   //事件监听
    func btnClick(sender:UIButton) {
        print(#function)
    }



}

上一篇 下一篇

猜你喜欢

热点阅读