如何获取snapkit为UIView声明的高度约束?

时间:2016-08-09 03:24:24

标签: swift snapkit

例如,我通过snapkit为UIView声明了一些约束:

timeProgress.snp_makeConstraints { (make) in
        make.left.equalTo(startTime.snp_right).offset(15)
        make.right.equalTo(endTime.snp_left).offset(-15)
        make.height.equalTo(5)
        make.centerY.equalTo(startTime)
    }

如何获得timeProgress的高度约束?

2 个答案:

答案 0 :(得分:3)

您可以保留对高度限制的引用:

var timeHeightConstraint: Constraint

timeProgress.snp_makeConstraints { (make) in
    make.left.equalTo(startTime.snp_right).offset(15)
    make.right.equalTo(endTime.snp_left).offset(-15)
    self.timeHeightConstraint = make.height.equalTo(5).constraint
    make.centerY.equalTo(startTime)
}

答案 1 :(得分:2)

您需要保留对约束的全局引用。

let timeProgressHeightConstant = 5.0

timeProgress.snp_makeConstraints { (make) in
    make.left.equalTo(startTime.snp_right).offset(15)
    make.right.equalTo(endTime.snp_left).offset(-15)
    make.height.equalTo(timeProgressHeightConstant)
    make.centerY.equalTo(startTime)
}

因此,您现在可以访问self.timeProgressHeightConstant