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
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
相关文章推荐
- Swift 使用Builder 文件 创建 Rings
- 使用 Swift 创建简单的二维码扫描应用
- swift中UIViewController的使用
- Swift3.0-UITextField创建以及使用
- 使用 Realm 和 Swift 创建 ToDo 应用
- 使用Swift在Xcode中创建自定义控件
- iOS swift使用xib绘制UIView
- iOS swift使用xib绘制UIView
- 分别使用OC和Swift语法创建单例
- 有关一些swift 控件的基本创建和使用
- Swift - 使用storyboard创建表格视图(TableViewController)
- swift -UIView的使用
- Swift-UITextField的创建和使用详解
- 【iOS(swift)笔记-3】swift使用SnapKit创建高度自适应的cell方法
- 直接使用XIB/NIB文件,读取信息创建 UIView [通过Nib文件进行反序列化,得到对象]
- Swift3.0语言教程使用占位符格式创建和初始化字符串
- Swift学习 接口的创建与使用
- iOS个人整理01-创建UI工程及UIView简单使用
- Swift3.0语言教程使用字符串创建和初始化字符串
- Swift中使用Xib获取UIView控件,结果UIView控件的subViews为nil