//
// 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.
}
*/
}