View操作 swift

时间:2023-03-09 02:45:07
View操作 swift
         //创建View
let view1 =UIView()
let view2 =UIView(frame: CGRectMake(,, ,))
let view3 =UIView(frame: CGRectMake(,, ,)) //设置view的尺寸
view1.frame =CGRectMake(,, ,) //设置view的背景色
view1.backgroundColor =UIColor.redColor()
view2.backgroundColor =UIColor.greenColor()
view3.backgroundColor = UIColor.blueColor() //设置view的中心位置,不改变view的大小
view1.center =CGPointMake(,) //改变view的宽和高,视图原来的中心位置不变
view1.bounds =CGRectMake(,, ,); //设置view的tag值
view1.tag =;
view2.tag =;
view3.tag =; //依次添加三个视图(从上到下是:蓝,绿,红)
self.view.addSubview(view1)
self.view.addSubview(view2)
self.view.addSubview(view3) //把view1(红)移到最上面
self.view.bringSubviewToFront(view1) //把view3(蓝)移到最下面
self.view.sendSubviewToBack(view3) //交换两个视图的位置
self.view.exchangeSubviewAtIndex(, withSubviewAtIndex: ) //把一个视图插在某个位置
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()as! UIView
view4.backgroundColor =UIColor.blackColor()
let view5 =self.view.viewWithTag()
view5?.backgroundColor =UIColor.purpleColor() //隐藏view1
view1.hidden =true; //删除View2
view2.removeFromSuperview() //再添加一个视图
let lastView =UIView()
lastView.frame =CGRectMake(,, ,);
lastView.backgroundColor =UIColor.init(white:0.80, alpha: )
self.view.addSubview(lastView) //设置view的透明度
lastView.alpha =0.5 //设置lastView的圆角角度
lastView.layer.cornerRadius =
//设置边框的的宽度
lastView.layer.borderWidth =
//设置边框的颜色
lastView.layer.borderColor =UIColor.redColor().CGColor
//允许剪切
lastView.clipsToBounds =true