• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Swift 控件约束之 SnapKit

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

一、附上 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

效果图:

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
对swift开发中的unwinding segue的一些理解发布时间:2022-07-13
下一篇:
swift class viewController has no initializers 的解决方法发布时间:2022-07-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap