swift中UIButton的使用

 func aa(){
        let btn:UIButton = UIButton.init(type: UIButton.ButtonType.custom);//新建btn
        btn.frame = CGRect.init(x: 10, y: 10,  100, height: 100);//frame位置和大小
        btn.backgroundColor = UIColor.red;//背景色
        btn.imageView?.image = UIImage.init(named: "aaa")//设置图片
        btn.layer.cornerRadius = 10
        btn.layer.masksToBounds = true //设置圆角
        btn.setTitle("点击", for:  UIControl.State.normal)//设置标题
        btn.titleLabel?.font = UIFont.systemFont(ofSize: 17)//设置字体大小
        self.view.addSubview(btn);
        //没有有参数
//        btn.addTarget(self, action: #selector(btnClick), for: UIControl.Event.touchUpInside);
        btn.addTarget(self, action:#selector(tapped(_:)), for:UIControl.Event.touchUpInside)
        
        
    }
//没有参数
//    @objc func btnClick(){
//        print("----------")
//    }
    @objc func tapped(_ btn:UIButton){
        print("----------")
    }
原文地址:https://www.cnblogs.com/hualuoshuijia/p/11639453.html