View操作 swift

 1         //创建View
 2         let view1 =UIView()
 3         let view2 =UIView(frame: CGRectMake(20,120, 100,100))
 4         let view3 =UIView(frame: CGRectMake(40,140, 100,100))
 5         
 6         //设置view的尺寸
 7         view1.frame =CGRectMake(0,100, 100,100)
 8         
 9         //设置view的背景色
10         view1.backgroundColor =UIColor.redColor()
11         view2.backgroundColor =UIColor.greenColor()
12         view3.backgroundColor = UIColor.blueColor()
13         
14         //设置view的中心位置,不改变view的大小
15         view1.center =CGPointMake(80,200)
16     
17         //改变view的宽和高,视图原来的中心位置不变
18         view1.bounds =CGRectMake(0,0, 40,40);
19         
20         //设置view的tag值
21         view1.tag =1;
22         view2.tag =2;
23         view3.tag =3;
24 
25         //依次添加三个视图(从上到下是:蓝,绿,红)
26         self.view.addSubview(view1)
27         self.view.addSubview(view2)
28         self.view.addSubview(view3)
29         
30         //把view1(红)移到最上面
31         self.view.bringSubviewToFront(view1)
32         
33         //把view3(蓝)移到最下面
34         self.view.sendSubviewToBack(view3)
35         
36         //交换两个视图的位置
37         self.view.exchangeSubviewAtIndex(0, withSubviewAtIndex: 2)
38          
39         //把一个视图插在某个位置
40         self.view.insertSubview(view1, atIndex:2)
41         
42         //把一个视图插在另一个视图的下面
43         self.view.insertSubview(view1, belowSubview: view3)
44         
45         //把一个视图插在另一个视图的上面
46         self.view.insertSubview(view1, aboveSubview: view2)
47          
48         //已经添加了某个视图
49         self.view.didAddSubview(view1)
50             
51         //将要移除某个视图
52         self.view.willRemoveSubview(view1)
53         
54         //把一个视图从一个父视图上移到另一个父视图上
55         self.view.willMoveToSuperview(view3)
56             
57         //已经移动到了父视图上
58         self.view.didMoveToSuperview()
59         
60         //把一个视图移动到一个窗口上
61         self.view.willMoveToWindow(UIApplication.sharedApplication().keyWindow)
62         
63         //已经移动到了一个窗口上
64         self.view.didMoveToWindow()
65             
66         //subViews中存放的(红,绿,蓝三个视图)
67         let subViews :NSArray = NSArray.init(array:self.view.subviews)
68         
69         //如何找到一个视图,其实此时view4就是view1,view5也是view1
70         let view4 = subViews.objectAtIndex(0)as! UIView
71         view4.backgroundColor =UIColor.blackColor()
72         let view5 =self.view.viewWithTag(1)
73         view5?.backgroundColor =UIColor.purpleColor()
74         
75         //隐藏view1
76         view1.hidden =true;
77         
78         //删除View2
79         view2.removeFromSuperview()
80         
81         //再添加一个视图
82         let lastView =UIView()
83         lastView.frame =CGRectMake(0,200, 200,200);
84         lastView.backgroundColor =UIColor.init(white:0.80, alpha: 1)
85         self.view.addSubview(lastView)
86         
87         //设置view的透明度
88         lastView.alpha =0.5
89 
90         //设置lastView的圆角角度
91         lastView.layer.cornerRadius =10
92         //设置边框的的宽度
93         lastView.layer.borderWidth =2
94         //设置边框的颜色
95         lastView.layer.borderColor =UIColor.redColor().CGColor
96         //允许剪切
97         lastView.clipsToBounds =true
原文地址:https://www.cnblogs.com/-yun/p/9066345.html