I'm building an iOS app against iOS 12 SDK, Swift 4 and SnapKit 4.2
I want to update a constraint when I tap a button but it creates a conflict with the previous version of the constraint.
Here's my code:
private var menuConstraint: Constraint?
override func updateViewConstraints() {
super.updateViewConstraints()
menuVc.view.snp.makeConstraints { (make) in
self.menuConstraint = make.top.equalTo(view.snp.top).constraint
make.right.equalTo(view.snp.right)
make.width.equalTo(100)
make.height.equalTo(100)
}
}
@objc func onMenuTap() {
self.menuConstraint!.update(offset: 100)
}
When onMenuTap
is called I get the following error:
[LayoutConstraints] Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don't want.
Try this:
(1) look at each constraint and try to figure out which you don't expect;
(2) find the code that added the unwanted constraint or constraints and fix it.
(
"<SnapKit.LayoutConstraint:[email protected]#77 UIView:0xDEF.top == UIView:0xGHI.top>",
"<SnapKit.LayoutConstraint:[email protected]#77 UIView:0xDEF.top == UIView:0xGHI.top + 100.0>"
)
As you can see the previous version of the top constraint (without the offset) is conflicting with the new version. It's as if it didn't update the existing constraint but instead just created a new one.
I've tried a few variations:
- wrapping the update line in a
snp.updateConstraints
closure - setting an initial offset when first creating the constraint
- grabbing the underlying
LayoutConstraint
and updatingconstant
directly.
I always get the same error message.
Do I have something configured wrong?
Don't put constraints inside
updateViewConstraints
as it'll recreate constraints as it's called multiple times so set the code insideviewDidLoad