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