美文网首页
UIView控件

UIView控件

作者: 112233香樟树 | 来源:发表于2016-11-18 17:29 被阅读0次

     1.ViewController.swift

    import UIKitclass ViewController: UIViewController {    override func viewDidLoad() {        super.viewDidLoad()       

     //获取当前控制器的view,设置背景颜色为红色     

       //self.view.backgroundColor = UIColor.red      

      //初始化方法,设置大小坐标       

     let rect = CGRect(x: 30, y: 30, width: 100, height: 200)        let subview:UIView = UIView(frame:rect)        

    //把子视图加载上父视图  

          subview.backgroundColor = UIColor.red        self.view.addSubview(subview)        

    //frame相对于父视图  

          let subview1 = UIView()        subview1.frame = CGRect(x: 140, y: 240, width: 100, height: 100)      subview1.backgroundColor = UIColor.yellow        self.view.addSubview(subview1)                      

      //center       

     let subview3 = UIView()        self.view.addSubview(subview3)        subview3.frame = CGRect(origin: self.view.center, size: CGSize(width: 20, height: 20))            subview3.backgroundColor = #colorLiteral(red: 0.3411764801, green: 0.6235294342, blue: 0.1686274558, alpha: 1)         

     //透明度       

     //subview3.alpha = 0.1  会影响视图        subview3.backgroundColor = UIColor(colorLiteralRed: 0.5, green: 0.5, blue: 0.5, alpha: 0.5)        let subview4 = UIView(frame: CGRect(x: 10, y: 10, width: 40, height: 40))        subview4.backgroundColor = #colorLiteral(red: 0.5725490451, green: 0, blue: 0.2313725501, alpha: 1)        subview3.addSubview(subview4)       

     //tag值使用2000以上,100以下系统用        subview4.tag = 10001        let Tagview = subview3.viewWithTag(10001)        print("subview4 = \(subview4),Tagview = \(Tagview)")                //subview4.isHidden = true

     //隐藏        //用户交互    

        self.view.isUserInteractionEnabled = true     

         //superview是父试图      print("subview = \(subview4.superview),subview3 = \(subview3)")    for item in self.view.subviews{            

    //从父试图上移除   

           // item.removeFromSuperview()            print(item)                            }            }    override func touchesBegan(_ touches: Set, with event: UIEvent?) {

    print("点击了当前控制器")

    }

    override func didReceiveMemoryWarning() {

    super.didReceiveMemoryWarning()

    }

    }

    相关文章

      网友评论

          本文标题:UIView控件

          本文链接:https://www.haomeiwen.com/subject/pkerpttx.html