let view = UIView(frame: CGRectMake(10.0,10.0,200.0,200.0)) // 添加到父视图 self.view.addSubview(view) // 背景颜色 view.backgroundColor = UIColor.redColor()
// layer层属性设置 view.layer.cornerRadius = 5.0 view.layer.borderColor = UIColor.greenColor().CGColor view.layer.borderWidth = 1.0 view.layer.masksToBounds = true
let view01 = UIView(frame: CGRectMake(10.0,50.0,50.0)) view.addSubview(view01) view01.backgroundColor = UIColor.yellowColor() let view02 = UIView(frame: CGRectMake(20.0,20.0,60.0,60.0)) view.addSubview(view02) view02.backgroundColor = UIColor.blueColor() let view03 = UIView(frame: CGRectMake(30.0,30.0,70.0,70.0)) view.addSubview(view03) view03.backgroundColor = UIColor.purpleColor() let view04 = UIView(frame: CGRectMake(10.0,70.0)) view04.backgroundColor = UIColor.orangeColor()
// 显示在最前面 view.bringSubviewToFront(view01) // 显示在最后面 view.sendSubviewToBack(view03) // 插入指定位置 view.insertSubview(view04,aboveSubview: view03) // 交换位置 view.exchangeSubviewAtIndex(2,withSubviewAtIndex: 3)
// 其他属性设置 view.userInteractionEnabled = true view.hidden = false view.alpha = 1.0
// 父视图 let superView = view.superview print(superView) // 子视图数组 let subViewArray = view.subviews print(subViewArray)
let currentView = view // 自定义view // let rect = CGRectMake(10.0,(CGRectGetMinY(currentView.frame) + CGRectGetHeight(currentView.bounds) + 10.0),300.0,40.0) let rect = CGRectMake(currentView.right,(currentView.bottom + 10.0),(currentView.width / 2),40.0) let syView = SYView.init(frame: rect) syView.height = 100.0 self.view.addSubview(syView) syView.backgroundColor = UIColor.greenColor() print(syView)
效果图如下所示
定论UIView的类扩展属性,即类似于Objective-C中使用left、right、top、bottom、width、height的属性。
创建swift文件,用于创建Extension类
import Foundation import UIKit extension UIView { // MARK: - left/right/top/bottom/width/height属性 /// 左间距 var left:CGFloat { get { return self.frame.origin.x } set(newValue) { var rect = self.frame rect.origin.x = newValue self.frame = rect } } /// 右间距 var right:CGFloat { get { return (self.frame.origin.x + self.frame.size.width) } set(newValue) { var rect = self.frame rect.origin.x = (newValue - self.frame.size.width) self.frame = rect } } /// 顶端间距 var top:CGFloat { get { return self.frame.origin.y } set(newValue) { var rect = self.frame rect.origin.y = newValue self.frame = rect } } /// 底端间距 var bottom:CGFloat { get { return (self.frame.origin.y + self.frame.size.height) } set(newValue) { var rect = self.frame rect.origin.y = (newValue - self.frame.size.height) self.frame = rect } } /// 长度 var width:CGFloat { get { return self.frame.size.width } set(newValue) { var rect = self.frame rect.size.width = newValue self.frame = rect } } /// 宽度 var height:CGFloat { get { return self.frame.size.height } set(newValue) { var rect = self.frame rect.size.height = newValue self.frame = rect } } }