首页 > 其他 > 详细

SnapKit自动布局的使用

时间:2019-11-28 10:22:41      阅读:73      评论:0      收藏:0      [点我收藏+]

这里只有一切基础的Demo,后期会继续更新的

  • 举例说明,创建一个红色UIView,居中,宽高都是50
        let redView : UIView = UIView.init()
        redView.backgroundColor = UIColor.red
        self.view.addSubview(redView)
        redView.snp.makeConstraints { (make) in
            make.width.height.equalTo(50) //宽度和高度设置为50
            make.center.equalTo(self.view)//在self.view上居中
        }
  • 创建一个红色UIView,顶部100,左右间距各10,高度50
        let redView : UIView = UIView.init()
        redView.backgroundColor = UIColor.red
        self.view.addSubview(redView)
        redView.snp.makeConstraints { (make) in
            make.height.equalTo(50)
            make.top.equalTo(100)
            make.left.equalTo(10)
            make.right.equalTo(-10)
        }

 

  • 创建一个蓝色的UIView,顶部在红色view的底部,做间距和宽度和红色view一样,高度是红色view的一半,以下两个demo是等价的
        let blue : UIView = UIView.init()
        blue.backgroundColor = UIColor.blue
        self.view.addSubview(blue)
        blue.snp.makeConstraints { (make) in
            make.top.equalTo(redView.snp.bottom)
            make.left.equalTo(redView.snp.left)
            make.width.equalTo((redView.snp.width))
            make.height.equalTo((redView.snp.height)).multipliedBy(0.5)
        }

 

       let blue : UIView = UIView.init()
        blue.backgroundColor = UIColor.blue
        self.view.addSubview(blue)
        blue.snp.makeConstraints { (make) in
            make.top.equalTo(redView.snp.bottom)
            make.left.equalTo(redView)
            make.width.equalTo((redView))
            make.height.equalTo((redView)).multipliedBy(0.5)
        }

 

  • 创建一个蓝色的UIView,left,top,bottom,right都和redView相等
        let blue : UIView = UIView.init()
        blue.backgroundColor = UIColor.blue
        self.view.addSubview(blue)
        blue.snp.makeConstraints { (make) in
            make.edges.equalTo(redView)
        }

 

  • 创建一个blueView,相对于父视图left,top,bottom,right都是10
      let blue : UIView = UIView.init()
        blue.backgroundColor = UIColor.blue
        self.view.addSubview(blue)
        blue.snp.makeConstraints { (make) in
            make.edges.equalTo(self.view).inset(UIEdgeInsets.init(top: 10, left: 10, bottom: 10, right: 10))
        }

 

  • 创建一个居中的lab,并设置最大或者最小的宽度
        let lab:UILabel = UILabel.init()
        lab.textAlignment = .center
        lab.backgroundColor = UIColor.red
        lab.numberOfLines = 0
        lab.text = "我是一个UILabel"
        self.view.addSubview(lab)
        lab.snp.makeConstraints { (make) in
            make.center.equalTo(self.view)
//            make.width.greaterThanOrEqualTo(200)//最小的宽度是200
            make.width.lessThanOrEqualTo(20)//最大的宽度是20
        }

 

SnapKit自动布局的使用

原文:https://www.cnblogs.com/hualuoshuijia/p/11944341.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!