Swift UIView常用方法

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
       
    
        let button = UIButton(type:.system);
        button.frame = CGRect.init(x:100,y:150,width:160,height:30);
        button.setTitle("changColor", for: .normal);
        button.backgroundColor = UIColor.brown;
        button.titleLabel?.font = UIFont.systemFont(ofSize: 17);
        button.tintColor = UIColor.cyan;
        button.addTarget(self, action: #selector(buttonDidClick), for: .touchUpInside);
        self.view.addSubview(button);
    
        let smallView = UIView();
        smallView.frame = CGRect.init(x: 50, y: 50, width: 100, height: 100);
        smallView.backgroundColor = UIColor.darkGray;
        smallView.tag = 100;
        self.view.addSubview(smallView);
        
    }
    func buttonDidClick() {
        print("按钮被点击了");
        let changView = self.view.viewWithTag(100);
        
        changView?.backgroundColor = UIColor.init(colorLiteralRed: Float(CGFloat(arc4random()%256)/255.0), green: Float(CGFloat(arc4random()%256)/255.0), blue: Float(CGFloat(arc4random()%256)/255.0), alpha: 1.0)
        
    }

}
详细代码查看:https://github.com/xiaolitou-ping/Swift-All

posted @ 2017-10-19 10:33  朝阳向日葵  阅读(1033)  评论(0编辑  收藏  举报