swift 基础小结02 -- VFL约束属性的get和set方法懒加载
Posted sunjianfei
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了swift 基础小结02 -- VFL约束属性的get和set方法懒加载相关的知识,希望对你有一定的参考价值。
一、属性的get和set方法
1、自定义属性的set和get方法
private(set) var _image:UIImage?
//自定义属性get,set
var image : UIImage?{
get{
return _image
}
set(newValue){
_image = newValue
self.imageView?.image = _image
}
}
2、重写父类属性的set和get方法
//重写父类属性get,set
override var tag:Int{
get{
return super.tag
}
set(newValue){
super.tag = newValue
self.button?.tag = newValue
}
二、VFL添加约束
和objective-c中相似,代码如下
let tabbarHeight:CGFloat = 49.0
func setupBaseUI(){
self.contentView.translatesAutoresizingMaskIntoConstraints = false
self.lineView.translatesAutoresizingMaskIntoConstraints = false
self.tabBar.translatesAutoresizingMaskIntoConstraints = false
let views = self.ConstraintDictionWithArray(nameArray: [self.contentView,self.lineView,self.tabBar,self.view], object: self);
NSLayoutConstraint.activate(NSLayoutConstraint.constraints(withVisualFormat: "H:|[contentView]|", options: NSLayoutFormatOptions.alignAllLeft, metrics: nil, views: views))
NSLayoutConstraint.activate(NSLayoutConstraint.constraints(withVisualFormat: "H:|[tabBar]|", options: NSLayoutFormatOptions.alignAllLeft, metrics: nil, views: views))
NSLayoutConstraint.activate(NSLayoutConstraint.constraints(withVisualFormat: "V:|[contentView][tabBar(tabbarHeight)]|", options: NSLayoutFormatOptions.alignAllLeft, metrics: ["tabbarHeight":(self.isIphoneX() ? tabbarHeight_iphoneX : tabbarHeight)], views: views))
NSLayoutConstraint.activate(NSLayoutConstraint.constraints(withVisualFormat: "H:|[lineView]|", options: NSLayoutFormatOptions.alignAllLeft, metrics: nil, views: views))
NSLayoutConstraint.activate(NSLayoutConstraint.constraints(withVisualFormat: "V:|[lineView(0.5)]", options: NSLayoutFormatOptions.alignAllLeft, metrics: nil, views: views))
}
//MARK: 相当于OC中的NSBinding(因为oc中没有宏定义,没有NSBinding这个宏定义,所以写一个方法代替这个方法)
func ConstraintDictionWithArray(nameArray:Array<UIView>,object:AnyObject) -> Dictionary<String,AnyObject> {
var dict:Dictionary<String,AnyObject> = [:]
var count:UInt32 = 0
let ivars = class_copyIvarList(object.classForCoder, &count)
for i in 0...Int(count) {
let obj = object_getIvar(object, ivars![i])
if let temp = obj as? UIView {
if nameArray.contains(temp){
let name = String.init(cString: ivar_getName(ivars![i])!)
dict[name] = temp
}
}
}
free(ivars)
return dict
}
三、懒加载
1、懒加载创建label
//MARK: 懒加载创建label
lazy var lazyLabel: UILabel = self.addLabel_d(title: "");
func addLabel_d(title:String) -> UILabel {
let label = UILabel(frame: CGRect.init(x: 0, y: 0, width: 200, height: 40))
label.center = self.view.center
label.backgroundColor = UIColor.gray
label.textColor = UIColor.white
label.textAlignment = NSTextAlignment.center
label.numberOfLines = 0
label.lineBreakMode = NSLineBreakMode.byWordWrapping
label.text = title
label.font = UIFont.systemFont(ofSize: 17)
return label;
}
2、懒加载数组
//lazy
lazy var datasource:NSArray = {
var tmpdatasource = NSArray(objects: ["title":"dispatch使用","content":["sync -- 同步","async -- 异步","delay -- 延迟执行三秒","main -- 主线程","global -- 全局并发队列"]],["title":"网络请求","content":["GET -- 请求","POST -- 请求","下载图片"]],["title":"自定义组件","content":["toast","。。。"]])
return tmpdatasource
}()
以上是关于swift 基础小结02 -- VFL约束属性的get和set方法懒加载的主要内容,如果未能解决你的问题,请参考以下文章
如果约束是使用 VFL 以编程方式创建的,如何为约束更改设置动画?