How to Simultaneously Satisfy Constraints Error Whats Causing This

swiftui - how to avoid 'Unable to simultaneously satisfy constraints' error?

Let's look at these one by one:

<NSLayoutConstraint:0x600002580d20 'assistantHeight' TUISystemInputAssistantView:0x7fde585064a0.height == 44 (active)>

This is saying view 0x7fde585064a0 is active and has 44 points height

<NSLayoutConstraint:0x600002591ae0 'assistantView.top' V:|-(0)-[TUISystemInputAssistantView:0x7fde585064a0] (active, names: '|':UIInputSetHostView:0x7fde58518070 )>

This is saying that 0x7fde585064a0's top edge must be a gap of 0 points from the top of its superview 0x7fde58518070

<NSLayoutConstraint:0x600002591b30 'assistantView.bottom' TUISystemInputAssistantView:0x7fde585064a0.bottom == _UIKBCompatInputView:0x7fde51b78c50.top (active)>

This is saying view 0x7fde51b78c50 and 0x7fde585064a0 are conflicting. These four things can't all be true. And the problem with constraints 0x600002591b30. Find out where the problem is and set true constraint.

I think it's useful to know the basics and understand what Apple / Xcode is trying to tell you via logs:

H = Horizontal constraint(for leading and Trailing)
V = Vertical constraint(top and bottom edge)
h = height
w = width

TopEdge -> V:|-(points)-[VIEW:memoryAddress]
BottomEdge -> V:[VIEW:memoryAddress]-(points)-|
Leading -> H:|-(points)-[VIEW:memoryAddress]
Trailing -> H:[VIEW:memoryAddress] -(points)-|
height -> h= --& v=--& V:[VIEW:memoryAddress((points)]
width -> VIEW:memoryAddress.width == points
between -> H:[VIEW 1]-(51)-[VIEW 2]

Unable to simultaneously satisfy constraints - No constraints in place

Let's look at these one by one.

"<NSAutoresizingMaskLayoutConstraint:0x84543d0 h=--& v=--& V:[UIView:0xa330270(768)]>"

This is saying view 0xa330270 (A) must be 768 points high.

"<NSLayoutConstraint:0xa338350 V:[UIView:0xa331260]-(-1)-| (Names: '|':UIView:0xa330270 )>"

This is saying view 0xa331260 (B)'s bottom edge must be a gap of -1 from the bottom of A, which is it's superview.

"<NSLayoutConstraint:0xa338390 V:|-(841)-[UIView:0xa331260] (Names: '|':UIView:0xa330270 )>"

This is saying that B's top edge must be a gap of 841 points from the top of its superview, A.

These three things can't all be true - A can't be 768 points high, and contain a subview with a top edge 841 points inset from the top and -1 points inset from the bottom. Where have you defined each of these constraints?

You haven't said what layout you are trying to achieve, but it looks like you might have an autoresizing mask on the superview that is preventing it changing in height when you rotate the device. As far as I know the autoresizing constraints only appear if you have added views programmatically, since a storyboard or xib is either all-autolayout, or not. Unless you are doing something like adding an auto laid out view (loaded from a nib?) to another view from a non-autolayout nib?

Unable to simultaneously satisfy constraints programmatically. Swift iOS

Every click causes a new conflict

var botCon:NSLayoutConstraint!

//

 botCon = someTableView.bottomAnchor.constraint(equalTo: view.layoutMarginsGuide.bottomAnchor, constant: -16)
botCon.isActive = true

//

@IBAction func firstButton(_ sender: Any) {
botCon.constant = -46
self.view.layoutIfNeeded()
}

@IBAction func secondButton(_ sender: Any) {
botCon.constant = -16
self.view.layoutIfNeeded()
}

Unable to simultaneously satisfy constraints - Swift

It would be a bit hard to track which is the bad constraint by just looking at the error you have posted. But I can give a general rules or things to keep in mind while working with autolayout or constraints in general,

  • Prefer setting constraints in XIB/Storyboards. It will tell where exactly are you going wrong right when you are setting them. You can almost do everything in the interface builder unless its a special case.
  • Always set constraints in relative to views around it try not fix everything to super View
  • Use aspect ratio constraints whenever possible.
  • Try to use Intrinsic constraint sizes of views and avoid setting fixed widths/heights.
  • Remember you will always get into the kind of errors you are facing, when you set too many constraints. So always think are the constraints "just enough" to figure out its frame in run time. Not more or less but just enough.
  • Make use of in-equality constraints, they are very helpful. Also don't forget you have hugging Priorities and compression resistance priorities.

In your error, I see a lot of constraints which have fixed sizes, they would interfere when you have spacing constraint to those views. So start for the first, and keep the points I mentioned and delete the constraints you don't need.

References,

  • Auto layout Programming Guide
  • Guide for Debugging Auto layout issues
  • Objc issue
  • Raywenderlich Guide
  • Some Tips

Unable to simultaneously satisfy constraints - No constraints in place

Let's look at these one by one.

"<NSAutoresizingMaskLayoutConstraint:0x84543d0 h=--& v=--& V:[UIView:0xa330270(768)]>"

This is saying view 0xa330270 (A) must be 768 points high.

"<NSLayoutConstraint:0xa338350 V:[UIView:0xa331260]-(-1)-| (Names: '|':UIView:0xa330270 )>"

This is saying view 0xa331260 (B)'s bottom edge must be a gap of -1 from the bottom of A, which is it's superview.

"<NSLayoutConstraint:0xa338390 V:|-(841)-[UIView:0xa331260] (Names: '|':UIView:0xa330270 )>"

This is saying that B's top edge must be a gap of 841 points from the top of its superview, A.

These three things can't all be true - A can't be 768 points high, and contain a subview with a top edge 841 points inset from the top and -1 points inset from the bottom. Where have you defined each of these constraints?

You haven't said what layout you are trying to achieve, but it looks like you might have an autoresizing mask on the superview that is preventing it changing in height when you rotate the device. As far as I know the autoresizing constraints only appear if you have added views programmatically, since a storyboard or xib is either all-autolayout, or not. Unless you are doing something like adding an auto laid out view (loaded from a nib?) to another view from a non-autolayout nib?

iOS AutoLayout issue: Unable to simultaneously satisfy constraints

I think you want to set the priority of these constraints to 999:

"<NSLayoutConstraint:0x7f8f7bdd74b0 H:|-(10)-[UIView:0x7f8f7bdbbe00]   (Names: '|':MarketGoodsDetail:0x7f8f7bdb92d0 )>",
"<NSLayoutConstraint:0x7f8f7bdd7500 H:[UIView:0x7f8f7bdbbe00]-(10)-| (Names: '|':MarketGoodsDetail:0x7f8f7bdb92d0 )>"

I believe those are conflicting with:

"<NSLayoutConstraint:0x7f8f7e0c6d90 'UIView-Encapsulated-Layout-Width' H:[UIView:0x7f8f7bdda860(0)]>"

Which specifies your container view's width is going to be 0 points. So it can't add the 10 point margins (which the two constraints above call for). Setting them to 999 should let them be overridden gracefully.

See What is a 'UIView-Encapsulated-Layout-Width' constraint? for more info.



Related Topics



Leave a reply



Submit