练习setContentCompressionResistancePriority

import UIKit
import SnapKit

let margin = 10
class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        // 设置UI
        setupUI()
    }

    func setupUI(){
    
        self.view.addSubview(contentView)
        contentView.addSubview(headImageView)
        contentView.addSubview(nameLable)
        contentView.addSubview(zanImageView)
        self.view.addSubview(siderView)
        
        contentView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self.view.snp_top).offset(margin*3)
            make.left.right.equalTo(self.view)
            make.height.equalTo(50)
        }
        
        headImageView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(contentView.snp_left).offset(margin)
            make.top.equalTo(contentView.snp_top).offset(margin)
            make.size.equalTo(CGSizeMake(30, 30))
        }
        
        nameLable.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(headImageView.snp_right).offset(margin)
            make.top.equalTo(contentView.snp_top).offset(margin)
            
        }
        
        zanImageView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(nameLable.snp_right).offset(margin)
            make.top.equalTo(contentView.snp_top).offset(margin)
            make.right.lessThanOrEqualTo(contentView.snp_right).offset(-margin)
        }
        
        siderView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentView.snp_bottom).offset(margin)
            make.left.equalTo(self.view.snp_left).offset(margin)
            make.right.equalTo(self.view.snp_right).offset(-margin)
            make.height.equalTo(10)
        }
    
    }
    
    // MARK - response
    func updateAdjustSliderValue(sender : UISlider){
    
        if sender.isKindOfClass(UISlider){
        
            let f = sender.value
            self.contentView.snp_updateConstraints { (make) -> Void in
                make.left.equalTo(self.view).offset(f)
            }
                
        }
    
    }
    
    // MARK - getter and setter
    // contentView
    private lazy var contentView : UIView = {
        let view = UIView()
        return view
    
    }()
    
    // 头像
    private lazy var headImageView: UIImageView  = {
        
        let imageView = UIImageView()
        imageView.image = UIImage(named: "get")
        return imageView
    
    }()
    
    // 姓名
    private lazy var nameLable : UILabel = {
    
        let lable = UILabel()
        lable.text = "测试AutoLayout优先级"
        lable.setContentCompressionResistancePriority(UILayoutPriorityDefaultLow, forAxis: UILayoutConstraintAxis.Horizontal)
        return lable
    }()
    
    // 点赞图片
    private lazy var zanImageView : UIImageView = {
    
        let zanImageVeiw = UIImageView()
        zanImageVeiw.image = UIImage(named: "money")
        zanImageVeiw.setContentCompressionResistancePriority(UILayoutPriorityDefaultHigh, forAxis: UILayoutConstraintAxis.Horizontal)
        return zanImageVeiw
    }()
    
    // sider
    private lazy var siderView : UISlider = {
      
        let sideView = UISlider()
        sideView.minimumValue = 10.0
        sideView.maximumValue = 200
        sideView.minimumTrackTintColor = UIColor.lightGrayColor()
        sideView.addTarget(self, action: Selector("updateAdjustSliderValue:"), forControlEvents: UIControlEvents.ValueChanged)
        
        return sideView
    }()
}


你可能感兴趣的:(练习setContentCompressionResistancePriority)