//
// VIEwController.swift
// Swift_003
// Created by 周双建 on 15/11/29.
// copyright © 2015年 周双建. All rights reserved.
//
//父视图和字视图
import UIKit
class VIEwController: UIVIEwController {
overrIDe func vIEwDIDLoad() {
super.vIEwDIDLoad()
//创建父视图
let Father_VIEw = UIVIEw(frame: CGRectMake(100,100,100))
Father_VIEw.backgroundcolor = UIcolor.redcolor()
self.vIEw.addSubvIEw(Father_VIEw)
//创建子视图
let Son_VIEw = UIVIEw(frame: CGRectMake(20,20,20))
Son_VIEw.backgroundcolor = UIcolor.greencolor()
Father_VIEw.addSubvIEw(Son_VIEw)
// Do any additional setup after loading the vIEw,typically from a nib.
//创建子视图2
let Son2_VIEw = UIVIEw(frame: CGRectMake(10,10,10))
Son2_VIEw.backgroundcolor = UIcolor.blackcolor()
Son_VIEw.addSubvIEw(Son2_VIEw)
//获取父视图的所有子视图
print(self.vIEw.subvIEws)
//获取Son_VIEw的所有子视图
print(Son_VIEw.subvIEws)
/********************************************************/
//添加子视图的其他方法
Son_VIEw.frame = CGRectMake(20, 20,100)
Son2_VIEw.frame = CGRectMake(20,100)
//第一种 将视图VIEw 置于子视图数组的下标为Index的位置
self.vIEw.insertSubvIEw(Son2_VIEw,atIndex: 2)
print(self.vIEw.subvIEws)
/* 打印结果
0 [<_UILayoutGuIDe: 0x7f98e0c56860; frame = (0 0; 0 0); hIDden = YES; layer = <CALayer: 0x7f98e0c55590>>,
1 <_UILayoutGuIDe: 0x7f98e0f24e80; frame = (0 0; 0 0); hIDden = YES; layer = <CALayer: 0x7f98e0f0cad0>>,0)"> 2 <UIVIEw: 0x7f98e0c55ab0; frame = (20 20; 100 100); layer = <CALayer: 0x7f98e0c55c20>>,0)"> 3 <UIVIEw: 0x7f98e0c555b0; frame = (100 100; 100 100); layer = <CALayer: 0x7f98e0c55720>>]
*/
//第二种 将视图VIEw 置于某个视图的下面
//首先调整son2坐标
Son2_VIEw.frame = CGRectMake(100, 100,100)
self.vIEw.insertSubvIEw(Son2_VIEw,belowSubvIEw: Son_VIEw)
// VIEw的隐藏 true false YES NO
Son_VIEw.hIDden = true
Son2_VIEw.hIDden = true
let Father1_vIEw = UIVIEw(frame: CGRectMake(120,120,100))
Father1_vIEw.backgroundcolor = UIcolor.magentacolor()
//给VIEw 设置 tag 标签
Father1_vIEw.tag = 100
self.vIEw.addSubvIEw(Father1_vIEw)
let VIEw_Common = UIVIEw(frame: CGRectMake(110,110,100))
VIEw_Common.backgroundcolor = UIcolor.blackcolor()
self.vIEw.insertSubvIEw(VIEw_Common,belowSubvIEw: Father1_vIEw)
/*
这是将 VIEw_Conmon 置于Father1_vIEw 的下面
/*******************************************************/
VIEw_Common.hIDden = true
let VIEw_one = UIVIEw(frame: CGRectMake(110,100))
VIEw_one.backgroundcolor = UIcolor.blackcolor()
self.vIEw.insertSubvIEw(VIEw_one,aboveSubvIEw: Father_VIEw)
/*
这是VIEw_one 置于 Father_VIEw 的上面
/*******************************************************/
VIEw_one.removeFromSupervIEw()
/*
VIEw 的移除
*/
}
/*
<UIVIEw: 0x7fc599613530; frame = (100 100; 100 100); layer = <CALayer: 0x7fc5996136a0>>]
*/
/*
[<UIVIEw: 0x7f8bb873c8f0; frame = (10 10; 10 10); layer = <CALayer: 0x7f8bb8705b00>>]
这说明命Son2_VIEw 不是Father_VIEw 的子视图
*/
overrIDe func touchesBegan(touches: Set<UItouch>,withEvent event: UIEvent?) {
var VIEw_Two = self.vIEw.vIEwWithTag(100)
VIEw_Two!.backgroundcolor = UIcolor.graycolor()
}
overrIDe func dIDReceiveMemoryWarning() {
super.dIDReceiveMemoryWarning()
// dispose of any resources that can be recreated.
}
}
总结以上是内存溢出为你收集整理的Swift UIView 的多种添加方法和打标签和移除(方法的详解)、父子视图的关系全部内容,希望文章能够帮你解决Swift UIView 的多种添加方法和打标签和移除(方法的详解)、父子视图的关系所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)