snapkit/Masonry动态修改约束

https://github.com/SnapKit/Masonry
https://github.com/SnapKit/SnapKit

一、SnapKit

1、持有约束
weak var topConstraint: Constraint? ;
topConstraint = make.top.equalTo(self.view.snp.bottom).constraint;
2约束停用或启用
topConstraint?.deactivate();
topConstraint?.activate();
3、获取约束的常量
let oldHeight: CGFloat = heightConstraint?.layoutConstraints.first?.constant ?? 0;
4、更新约束常量
heightConstraint?.layoutConstraints.first?.constant = 300;

二、Masonry

1、持有约束
@property(nonatomic, strong) MASConstraint * topConstraint;
self.topConstraint = make.top.mas_equalTo(self.view.mas_bottom).constraint;
2约束停用或启用
[self.topConstraint deactivate];
[self.topConstraint activate];
3、获取约束的常量
NSLayoutConstraint *layoutConstraint = [self.topConstraint valueForKey:@"layoutConstraint"];
CGFloat oldHeight = layoutConstraint.constant;
4、更新约束常量
NSLayoutConstraint *layoutConstraint = [self.topConstraint valueForKey:@"layoutConstraint"];
layoutConstraint.constant = 300;


具体代码如下:

import UIKit
import SnapKit

class Test1ViewController: UIViewController {

    lazy var redView: UIView = {
        let v = UIView(frame: CGRect(x: 0, y: self.view.bounds.height, width: self.view.bounds.width, height: 500))
        v.backgroundColor = UIColor.red
        let pan = UIPanGestureRecognizer(target: self, action: #selector(panMove(gesture:)))
        v.addGestureRecognizer(pan)
        return v
    }()
    
    weak var topConstraint: Constraint?
    weak var bottomConstraint: Constraint?
    weak var heightConstraint: Constraint?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = UIColor.white
        let btn1 = UIButton(frame: CGRect(x: 20, y: 150, width: 60, height: 40))
        btn1.backgroundColor = .green
        btn1.setTitle("show", for: .normal)
        btn1.addTarget(self, action: #selector(show(_:)), for: .touchUpInside)
        self.view.addSubview(btn1)
        
        let btn2 = UIButton(frame: CGRect(x: 120, y: 150, width: 60, height: 40))
        btn2.backgroundColor = .green
        btn2.setTitle("close", for: .normal)
        btn2.addTarget(self, action: #selector(close), for: .touchUpInside)
        self.view.addSubview(btn2)
        
        let btn3 = UIButton(frame: CGRect(x: 190, y: 150, width: 120, height: 40))
        btn3.backgroundColor = .green
        btn3.setTitle("updateHeight", for: .normal)
        btn3.addTarget(self, action: #selector(updateHeight), for: .touchUpInside)
        self.view.addSubview(btn3)
        
        self.view.addSubview(redView)
        redView.snp.makeConstraints { make in
            topConstraint = make.top.equalTo(self.view.snp.bottom).constraint
            make.left.right.equalTo(0)
            heightConstraint = make.height.equalTo(500).constraint
        }
    }

    

    @objc func show(_ sender: UIButton) {
        topConstraint?.deactivate()
        topConstraint = nil
        if bottomConstraint != nil {
            bottomConstraint?.activate()
        }else {
            redView.snp.makeConstraints { make in
                bottomConstraint = make.bottom.equalTo(self.view.snp.bottom).constraint
            }
        }
        
        
        UIView.animate(withDuration: 0.3) {
            self.redView.superview?.layoutIfNeeded()
        }
    }
    
    @objc func close() {
        bottomConstraint?.deactivate()
        bottomConstraint = nil
        if topConstraint != nil {
            topConstraint?.activate()
        }else {
            redView.snp.makeConstraints { make in
                topConstraint = make.top.equalTo(self.view.snp.bottom).constraint
            }
        }
        UIView.animate(withDuration: 0.3) {
            self.redView.superview?.layoutIfNeeded()
        }
    }
    
    @objc func panMove(gesture: UIPanGestureRecognizer) {
        if gesture.state == .changed {
            let point = gesture.translation(in: gesture.view?.superview)
            let oldHeight: CGFloat = heightConstraint?.layoutConstraints.first?.constant ?? 0
            let toHeight = oldHeight - point.y
            heightConstraint?.layoutConstraints.first?.constant = toHeight
            
            UIView.animate(withDuration: 0.3) {
                self.redView.superview?.layoutIfNeeded()
            }
            
            gesture.setTranslation(.zero, in: gesture.view?.superview)
        }
    }
    
    @objc func updateHeight() {
        let oldHeight: CGFloat = heightConstraint?.layoutConstraints.first?.constant ?? 0
        heightConstraint?.layoutConstraints.first?.constant = oldHeight + 10
        
        UIView.animate(withDuration: 0.3) {
            self.redView.superview?.layoutIfNeeded()
        }
    }
}

工具分类:

import SnapKit

extension Constraint {
    var constant: CGFloat {
        get {
            return self.layoutConstraints.first?.constant ?? 0
        }
        set {
            self.layoutConstraints.first?.constant = newValue
        }
    }
}

extension ConstraintMakerFinalizable {
    var constant: CGFloat {
        get {
            return self.constraint.constant
        }
        set {
            self.constraint.constant = newValue
        }
    }
    
    func active() {
        self.constraint.activate()
    }
    
    func deactivate() {
        self.constraint.deactivate()
    }
}



#import <Masonry/Masonry.h>

NS_ASSUME_NONNULL_BEGIN

@interface MASConstraint (Wonderful)
@property(nonatomic, assign) CGFloat constant;
@end

NS_ASSUME_NONNULL_END
#import "MASConstraint+Wonderful.h"

@implementation MASConstraint (Wonderful)
- (CGFloat)constant {
    NSLayoutConstraint *layoutConstraint = [self valueForKey:@"layoutConstraint"];
    return layoutConstraint.constant;
}

- (void)setConstant:(CGFloat)constant {
    NSLayoutConstraint *layoutConstraint = [self valueForKey:@"layoutConstraint"];
    layoutConstraint.constant = constant;
}


@end
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容