Unexpected behavior when using `greaterThanOrEqualTo` and `inset`
New Issue Checklist
- [X] I have looked at the Documentation
- [X] I have read the F.A.Q.
- [X] I have filled out this issue template.
Issue Info
| Info | Value |
|---|---|
| Platform | iOS |
| Platform Version | 12.1 |
| SnapKit Version | 4.0.0 |
| Integration Method | CocoaPods |
Issue Description
Probably just me being caught off guard on that one, but I was expecting greaterThanOrEqualToSuperview(), and by extension greaterThanOrEqualTo() and related methods, to behave the same as inset() when used together.
Take this:
label.snp.makeConstraints { (make) in
make.edges.greaterThanOrEqualToSuperview().inset(UIEdgeInsets(top: 24, left: 8, bottom: 24, right: 8))
make.center.equalToSuperview()
}
The effect I was after was along the line of: I want my right edge to be inset by 8 pt or more. But what ended up happening was: the right edge was constraints to -8 pt or more from its parent right edge. This lead the inner view to use all the space within the insets of its superview instead of shrinking to its content size while being centered.
This behavior makes sense after thinking about it, but it's not straightforward in regard to all inset values being positive.
What I should have used instead (which defeats the purpose of using SnapKit IMO):
label.snp.makeConstraints { (make) in
make.top.greaterThanOrEqualToSuperview().offset(24)
make.left.greaterThanOrEqualToSuperview().offset(8)
make.bottom.lessThanOrEqualToSuperview().offset(-24)
make.right.lessThanOrEqualToSuperview().offset(-8)
make.center.equalToSuperview()
}
Am I missing something?
I understand that the behavior of greaterThanOrEqualToSuperview() & co cannot be changed now that it's probably being used in hundreds of apps. But would it make sense to build a new API with the above behavior?
+1
@pquillere You need to use both lessThanOrEqualTo and greaterThanOrEqualTo because the constraints put the anchors (the view's edges) into relation, and not the spacing therebetween.
You can however stick to using your UIEdgeInsets like this:
let insets = UIEdgeInsets(top: 24, left: 8, bottom: 24, right: 8)
label.snp.makeConstraints { (make) in
make.top.left.greaterThanOrEqualToSuperview().inset(insets)
make.right.bottom.lessThanOrEqualToSuperview().inset(insets)
make.center.equalToSuperview()
}