Reputation: 35
Let's assume CustomView's
size is 300x300. iconImageView
has its size and assigned constraints. I do not know how long is going to be text in UILabel
so I do not want to make constant size of UILabel
. My goal is to pin left constraint to right side of the iconImageView
and right to customView
.
override func updateConstraints() {
super.updateConstraints()
iconImageView.snp.updateConstraints { (make) in
make.left.equalTo(customView).offset(10)
make.centerY.equalTo(customView)
make.size.equalTo(CGSize(width: 40.0, height: 40.0))
}
nameLabel.snp.updateConstraints { (make) in
make.right.equalTo(customView).offset(-10)
make.left.equalTo(iconImageView.snp.right).offset(10)
make.centerY.equalTo(customView)
}
}
When I try this method I get error: Unable to simultaneously satisfy constraints.
What's a proper way to do this?
Upvotes: 1
Views: 6528
Reputation: 141
Well, I suppose your subview don't know anything about top/bottom constrains which means the view don't know how to re-layout itself. Try this one:
override func updateConstraints() {
super.updateConstraints()
iconImageView.snp.updateConstraints { (make) in
make.left.equalTo(customView).offset(10)
make.centerY.equalTo(customView)
// Also from my point of view this line \/
// is not very readable
// make.size.equalTo(CGSize(width: 40.0, height: 40.0))
// Changed to:
make.width.height.equalTo(40.0)
}
nameLabel.snp.updateConstraints { (make) in
make.right.equalTo(customView).offset(-10)
make.left.equalTo(iconImageView.snp.right).offset(10)
// Add:
make.top.equalTo(customView.snp.top)
make.bottom.equalTo(customView.snp.bottom)
}
}
If you want to keep "default" height of your label (in case of empty strings etc.) you can add:
make.height.greaterThanOrEqual(40.0)
also autolayout and frames don't work well with each other, so you should layout your custom view in "updateConstraints" method, similar to this:
customView.snp.updateConstraints { (make) in
make.edges.equalTo(self)
}
Upvotes: 3