码迷,mamicode.com
首页 > 编程语言 > 详细

swift 基础小结02 -- VFL约束、属性的get和set方法、懒加载

时间:2018-08-13 14:58:49      阅读:214      评论:0      收藏:0      [点我收藏+]

标签:main   line   data   format   ola   private   form   lazy   sip   

一、属性的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方法、懒加载

标签:main   line   data   format   ola   private   form   lazy   sip   

原文地址:https://www.cnblogs.com/sunjianfei/p/9467860.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!