SnapKitを使って勉強します

5950 ワード

使用上の注意:最初にコントロールを親ビューに追加し、制約を設定します.
一、基本的な使用実践
      let redview = UIView()
        redview.backgroundColor = UIColor.red
        view.addSubview(redview)
        redview.snp.makeConstraints { (make) in
            make.width.height.equalTo(100)//     100
            make.top.equalTo(view.snp.top).offset(100)//                100
            make.centerX.equalTo(self.view.snp.centerX)//    x             
        }
      let redview = UIView()
        redview.backgroundColor = UIColor.red
        view.addSubview(redview)
          redview.snp.makeConstraints { (make) in
            make.size.equalTo(CGSize(width: 100, height: 100))//     100
            make.top.equalToSuperview().offset(100) //                100
            make.centerX.equalToSuperview()//    x            
        }
      let redview = UIView()
        redview.backgroundColor = UIColor.red
        view.addSubview(redview)
        redview.snp.makeConstraints { (make) in
               make.size.equalTo(100) //     100
               make.top.equalToSuperview().offset(100)
               make.centerX.equalToSuperview()
        }

 let redview = UIView()
         redview.backgroundColor = UIColor.red
        view.addSubview(redview)
        redview.snp.makeConstraints { (make) in
            make.leading.equalToSuperview().offset(30)
            make.trailing.equalToSuperview().offset(-30)
            make.top.equalToSuperview().offset(100)
            make.height.equalTo(40)
        }
let redview = UIView()
        redview.backgroundColor = UIColor.red
       view.addSubview(redview)
       redview.snp.makeConstraints { (make) in
           make.width.equalToSuperview().dividedBy(2)//        1/2
           make.centerX.equalToSuperview()
           make.top.equalToSuperview().offset(100)
           make.height.equalTo(60)
       }

let redview = UIView()
        redview.backgroundColor = UIColor.red
       view.addSubview(redview)
        redview.snp.makeConstraints { (make) in
           make.width.equalTo(100)
           make.centerX.equalToSuperview()
           make.top.equalToSuperview().offset(100)
           make.height.equalTo(redview.snp.width).multipliedBy(2)//     2 
       }
let redview = UIView()
        redview.backgroundColor = UIColor.red
       view.addSubview(redview)
 redview.snp.makeConstraints { (make) in
           make.width.equalTo(100);
           make.centerX.equalToSuperview()
           make.top.equalToSuperview().offset(100);
      make.bottom.equalTo(redview.snp.top).multipliedBy(3)
           
       }
       let redview = UIView()
       redview.backgroundColor = UIColor.red
       view.addSubview(redview)
       redview.snp.makeConstraints { (make) in
       make.leading.top.equalToSuperview().offset(100)
       make.right.bottom.equalTo(view).offset(-100)
       }
        let redview = UIView()
        redview.backgroundColor = UIColor.red
        view.addSubview(redview)
        redview.snp.makeConstraints { (make) in
        make.leading.equalTo(view).offset(100)
        make.trailing.equalTo(view).offset(-100)
        make.top.equalTo(view.snp.top).offset(100)
        make.bottom.equalToSuperview().offset(-100)
        }
       let redview = UIView()
        redview.backgroundColor = UIColor.red
        view.addSubview(redview)
 let redview = UIView()
        redview.backgroundColor = UIColor.red
        view.addSubview(redview)
        redview.snp.makeConstraints { (make) in
          make.edges.equalTo(UIEdgeInsetsMake(100, 100, 100, 100))
        }
      
二、UICLable適応
  • 1、縦または水平方向の制約がないので、幅または高適応
  •     let lable = UILabel()
            lable.backgroundColor=UIColor.yellow
            lable.numberOfLines = 0
            view.addSubview(lable)
            lable.text =
            "         ,         ,                ,         ,                ,         ,       "
            lable.snp.makeConstraints { (make) in
            make.top.leading.equalToSuperview().offset(100)
            make.width.equalTo(100)
            }
    
    
  • 、1つの範囲内の制約適応
  •       let lable = UILabel()
            lable.backgroundColor=UIColor.yellow
            lable.numberOfLines = 0
            view.addSubview(lable)
            lable.text =
            "  "
            lable.snp.makeConstraints { (make) in
            make.top.leading.equalToSuperview().offset(100)
            make.width.greaterThanOrEqualTo(100)//       100
            make.width.lessThanOrEqualTo(200)//    100, 200         ,       200
            }
    
    三、親のコントロールの高さは、サブコントロールの高さによって変わります.
    1、サブコントロール制約の設定
    import UIKit
    import SnapKit
    class LogView: UIView {
        override init(frame: CGRect) {
            super.init(frame: frame)
            let imageview = UIImageView()
            imageview.backgroundColor = UIColor.yellow
            imageview.image = UIImage(named: "visitordiscover_image_message")
            self.addSubview(imageview)
            imageview.snp.makeConstraints { (make) in
                make.top.equalToSuperview().offset(30)
                make.width.equalTo(100)
                make.centerX.equalToSuperview()
                make.height.equalTo(100)//       
                make.bottom.equalToSuperview().offset(-30)//           
            }
           
        }
        required init?(coder aDecoder: NSCoder) {
            fatalError("init(coder:) has not been implemented")
        }
    }
    
    2、親のコントロール制約設定
      override func viewDidLoad() {
            super.viewDidLoad()
            let logview = LogView()
            logview.backgroundColor = UIColor.green
            view.addSubview(logview)
            logview.snp.makeConstraints { (make) in
              make.centerY.equalTo(self.view)
              make.left.right.equalTo(self.view)
             //       ,            
            }
        }