您的位置:首页 > 移动开发 > Swift

Swift-UIView的创建和使用

2016-09-21 09:06 337 查看
//创建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.backgroundColor = UIColor.blueColor()

        

        //设置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(view1)

        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:2)

        

       
//把一个视图插在另一个视图的下面

        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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息