UIButton的创建 注意点 事件监听

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)
    }



}

你可能感兴趣的:(UIButton的创建 注意点 事件监听)