swift 操作View图层

//

//  FatherOrSons.swift

//  buttonDemo

//

//  Created by zx on 2018/10/28.

//  Copyright © 2018年 zx. All rights reserved.

//

 

import UIKit

 

class FatherOrSons: UIViewController {

 

    override func viewDidLoad() {

        super.viewDidLoad()

 

        view.backgroundColor = UIColor.white

        //创建父视图

        let  Father_View = UIView(frame: CGRect(x:100,y:100,width:100,height:100))

        Father_View.backgroundColor = UIColor.red

        self.view.addSubview(Father_View)

        

        //创建子视图

        let  Son_View = UIView(frame: CGRect(x:20,y:20,width:20,height:20))

        Son_View.backgroundColor = UIColor.green

        Father_View.addSubview(Son_View)

        // Do any additional setup after loading the view, typically from a nib.

        //创建子视图2

        let  Son2_View = UIView(frame: CGRect(x:10,y:10,width:10,height:10))

        Son2_View.backgroundColor = UIColor.orange

        Son_View.addSubview(Son2_View)

        

        //获取父视图的所有子视图

        print(self.view.subviews)

        //获取Son_View的所有子视图

        print(Son_View.subviews)

        

        //添加子视图的其他方法

        Son_View.frame = CGRect(x: 20, y: 20, width: 100, height: 100)

        Son2_View.frame = CGRect(x: 20, y: 20, width: 100, height: 100)

        

        //第一种  将视图View 置于子视图数组的下标为at的位置

        self.view.insertSubview(Son2_View, at:2)

        print(self.view.subviews)

        

        

        //第二种  将视图View 置于某个视图的下面

        //首先调整son2坐标

        Son2_View.frame = CGRect(x: 100, y: 100, width: 100, height: 100)

        self.view.insertSubview(Son2_View, belowSubview: Son_View)

        

        //  View的隐藏    true   false   YES   NO

        Son_View.isHidden = false

        Son2_View.isHidden = false

        

        

        let  Father1_view = UIView(frame: CGRect(x:120,y:120,width:100,height:100))

        Father1_view.backgroundColor = UIColor.magenta

        //给View设置  tag  标签

        Father1_view.tag = 100

        self.view.addSubview(Father1_view)

        

        

        let  View_Common = UIView(frame: CGRect(x:110,y:110,width:100,height:100))

        View_Common.backgroundColor = UIColor.black

        self.view.insertSubview(View_Common, belowSubview: Father1_view)

        print(self.view.subviews)

        /*

         这是将  View_Conmon置于Father1_view的下面

         */

        

        /*******************************************************/

        View_Common.isHidden = true

        

        let  View_one = UIView(frame: CGRect(x:110,y:110,width:100,height:100))

        View_one.backgroundColor = UIColor.black

        self.view.insertSubview(View_one, aboveSubview: Father_View)

        /*

         这是View_one置于 Father_View的上面

         */

        

        

        /*******************************************************/

        View_one.removeFromSuperview()

        /*

         View 的移除

         */

    }

    

 

    /*

    // MARK: - Navigation

 

    // In a storyboard-based application, you will often want to do a little preparation before navigation

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

        // Get the new view controller using segue.destination.

        // Pass the selected object to the new view controller.

    }

    */

 

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值