swift 5.0 创建button方法


class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    
        //创建视图
        let redV = UIView(frame: CGRect(x: 100, y: 100, width: 100, height: 100))
        
        //设置视图的背景颜色
        redV.backgroundColor = UIColor.red;
        
        //添加到当前页面
        view.addSubview(redV);
        redV.center = view.center;
    
        
        let btn = UIButton(type: .contactAdd);
        redV .addSubview(btn);

        
        btn.addTarget(self, action: #selector(btnClicked), for: .touchUpInside);
        
    }

    @objc func btnClicked()->String{
        print(#function);
        return "111";
    }
}

 swift 创建 imageview 

    let img = UIImageView(frame: CGRect(x: 100, y: 300, width: 50, height: 50))
    view .addSubview(img)
    img.image = UIImage.init(named: "22");    

  


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM