Swift-UIView的建立和使用
//建立View
let view1 =UIView()
let view2 =UIView(frame: CGRectMake(20,120, 100,100))
let view3 =UIView(frame: CGRectMake(40,140, 100,100))
//設定view的尺寸
view1.frame =CGRectMake(0,100, 100,100)
//設定view的背景色
view1.backgroundColor =UIColor.redColor()
view2.backgroundColor =UIColor.greenColor()
view3.
//設定view的中心位置,不改變view的大小
view1.center =CGPointMake(80,200)
//改變view的寬和高,檢視原來的中心位置不變
view1.bounds =CGRectMake(0,0, 40,40);
//設定view的tag值
view1.tag =1;
view2.tag =2;
view3.tag =3;
//依次新增三個檢視(從上到下是:藍,綠,紅)
self.view.addSubview
self.view.addSubview(view2)
self.view.addSubview(view3)
//把view1(紅)移到最上面
self.view.bringSubviewToFront(view1)
//把view3(藍)移到最下面
self.view.sendSubviewToBack(view3)
//交換兩個檢視的位置
self.view.exchangeSubviewAtIndex(0, withSubviewAtIndex: 2)
//把一個檢視插在某個位置
self.view.insertSubview(view1, atIndex:
//把一個檢視插在另一個檢視的下面
self.view.insertSubview(view1, belowSubview: view3)
//把一個檢視插在另一個檢視的上面
self.view.insertSubview(view1, aboveSubview: view2)
//已經添加了某個檢視
self.view.didAddSubview(view1)
//將要移除某個檢視
self.view.willRemoveSubview(view1)
//把一個檢視從一個父檢視上移到另一個父檢視上
self.view.willMoveToSuperview(view3)
//已經移動到了父檢視上
self.view.didMoveToSuperview()
//把一個檢視移動到一個視窗上
self.view.willMoveToWindow(UIApplication.sharedApplication().keyWindow)
//已經移動到了一個視窗上
self.view.didMoveToWindow()
//subViews中存放的(紅,綠,藍三個檢視)
let subViews :NSArray = NSArray.init(array:self.view.subviews)
//如何找到一個檢視,其實此時view4就是view1,view5也是view1
let view4 = subViews.objectAtIndex(0)as! UIView
view4.backgroundColor =UIColor.blackColor()
let view5 =self.view.viewWithTag(1)
view5?.backgroundColor =UIColor.purpleColor()
//隱藏view1
view1.hidden =true;
//刪除View2
view2.removeFromSuperview()
//再新增一個檢視
let lastView =UIView()
lastView.frame =CGRectMake(0,200, 200,200);
lastView.backgroundColor =UIColor.init(white:0.80, alpha: 1)
self.view.addSubview(lastView)
//設定view的透明度
lastView.alpha =0.5
//設定lastView的圓角角度
lastView.layer.cornerRadius =10
//設定邊框的的寬度
lastView.layer.borderWidth =2
//設定邊框的顏色
lastView.layer.borderColor =UIColor.redColor().CGColor
//允許剪下
lastView.clipsToBounds =true