Swift UIView 的多种添加方法和打标签和移除(方法的详解)、父子视图的关系

前端之家收集整理的这篇文章主要介绍了Swift UIView 的多种添加方法和打标签和移除(方法的详解)、父子视图的关系前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

//

// 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()

@H_502_51@//创建父视图

let Father_View = UIView(frame: CGRectMake(100,100,100))

Father_View.backgroundColor = UIColor.redColor()

self.view.addSubview(Father_View)

@H_502_51@//创建子视图

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)

@H_502_51@/*

这是将 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)

@H_502_51@/*

这是View_one 置于 Father_View 的上面

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

View_one.removeFromSuperview()

@H_502_51@/*

View 的移除

*/

}

/*

<UIView: 0x7fc599613530; frame = (100 100; 100 100); layer = <CALayer: 0x7fc5996136a0>>]

*/

@H_502_51@/*

[<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.

}



}

原文链接:https://www.f2er.com/swift/325288.html

猜你在找的Swift相关文章