I have a view with two leading constraints that conflict each other. One with leading offset, other without. The 'constant' property works for active constraint. But I want to enable/disable constraints to fit my needs. It works in every place except UITableViewCell
, when called from tableView:cellForRowAtIndexPath:
.
Here is my cell:
class WowCell: UITableViewCell {
@IBOutlet weak var myView: UIView!
@IBOutlet var leading: NSLayoutConstraint!
@IBOutlet var leadingSpace: NSLayoutConstraint!
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
func setup(ye:Bool) {
setActiveConstraint(ye: ye)
//doesn't help
setNeedsLayout()
layoutIfNeeded()
needsUpdateConstraints()
setNeedsUpdateConstraints()
needsUpdateConstraints()
setNeedsLayout()
setNeedsDisplay()
}
func setActiveConstraint(ye:Bool){
leading.isActive = !ye
leadingSpace.isActive = ye
}
override func layoutSubviews() {
//when called from here, it does work
}
}
Thanks in advance
Edit: As Prashant Tukadiya advised, it works by changing priorities of both active constraints. But be aware that both constraint have to be lower than 1000.
func setActiveConstraint(ye:Bool){
leading.priority = UILayoutPriority(rawValue: !ye ? 999 : 250)
leadingSpace.priority = UILayoutPriority(rawValue: ye ? 999 : 250)
}