Swift 控制項約束之 SnapKit

來源:互聯網
上載者:User

標籤:bsp   set   ...   地址   約束   head   contract   hid   targe   

一、附上 SnapKit 在 github 的地址:https://github.com/SnapKit/SnapKit

官方指導添加約束:https://github.com/SnapKit/SnapKit#usage 

SnapKit 3.0 Migration Guide: https://github.com/SnapKit/SnapKit/blob/master/Documentation/SnapKit%203.0%20Migration%20Guide.md

 

二、具體使用

Swift 3.0 使用懶載入

 //頭像    lazy var headerImageView: UIImageView = {        let imgView: UIImageView = UIImageView()        imgView.backgroundColor = UIColor.red        return imgView    }()        //title    lazy var titleLabel: UILabel = {        let time: UILabel = UILabel()        time.font = UIFont.boldSystemFont(ofSize: 15)         time.text = "工作通知:iOS"        return time    }()        //時間    lazy var timeLabel: UILabel = {        let time: UILabel = UILabel()                time.font = UIFont.systemFont(ofSize: 10)        time.textColor = UIColor.lightGray        time.text = "昨天"        time.textAlignment = .right        time.baselineAdjustment = .none        return time    }()            //描述    lazy var descriptionLabel: UILabel = {        let time: UILabel = UILabel()                time.font = UIFont.systemFont(ofSize: 13)        time.textColor = UIColor.lightGray        time.text = "[考勤打卡] 還有六分鐘就要上班了,..."        time.textAlignment = .left        time.baselineAdjustment = .none        return time    }()        //未處理時間1    lazy var noHandleLabel: UILabel = {        let time: UILabel = UILabel()        time.isHidden = true        time.backgroundColor = .red        time.font = UIFont.systemFont(ofSize: 10)        time.textColor = UIColor.white        time.text = "1"        time.textAlignment = .center        time.baselineAdjustment = .none        return time    }()
View Code

使用 Snapkit 給控制項添加約束

 func addSubViews() -> Void {                self.addSubview(self.headerImageView)        self.addSubview(self.titleLabel)        self.addSubview(self.timeLabel)        self.addSubview(self.descriptionLabel)        self.addSubview(self.noHandleLabel)                //頭像                self.headerImageView.snp.makeConstraints { (make) in            make.left.equalToSuperview().offset(10)            make.top.equalToSuperview().offset(10)            make.bottom.equalToSuperview().offset(-10)            make.width.equalTo(self.headerImageView.snp.height)        }                                                //title                self.titleLabel.snp.makeConstraints { (make) in            make.top.equalTo(self.headerImageView)            make.left.equalTo(self.headerImageView.snp.right).offset(10)                    }                //時間                self.timeLabel.snp.makeConstraints { (make) in            //make.top.equalTo(self.titleLabel.snp.top)            make.bottom.equalTo(self.titleLabel.snp.bottom)            make.right.equalToSuperview().offset(-10)        }                //描述               self.descriptionLabel.snp.makeConstraints { (make) in            make.left.equalTo(self.titleLabel.snp.left).offset(0)            make.bottom.equalTo(self.headerImageView.snp.bottom).offset(0)            make.right.equalTo(self.noHandleLabel).offset(-20)        }                //未處理事件        self.noHandleLabel.snp.makeConstraints { (make) in            make.right.equalTo(self.timeLabel.snp.right).offset(0)            make.bottom.equalTo(self.descriptionLabel.snp.bottom).offset(0)            make.top.equalTo(self.descriptionLabel.snp.top).offset(0)            make.width.equalTo(self.noHandleLabel.snp.height).offset(0)        }    }
View Code

 

Swift 控制項約束之 SnapKit

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.