kean/Align

Unable to simultaneously satisfy constraints.

murselturk opened this issue · 3 comments

Hey,

addSubview(bgView) {
    $0.edges.pinToSuperview()
}

addSubview(searchBar) {
    $0.edges(.left, .right, .top).pinToSuperview()
    $0.height.set(44)
}

addSubview(tableView) {
    $0.edges(.left, .right, .bottom).pinToSuperview()
    $0.height.match(bgView.al.height - 44)
}

When I run this code, I get the following output, and I'm not sure what I'm doing wrong:

[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. 
(
    "<NSLayoutConstraint:0x6080000944b0 UIImageView:0x7f87650162c0.bottom == Testing.MyTableView:0x7f8765010b70.bottom   (active)>",
    "<NSLayoutConstraint:0x6080000945a0 V:|-(0)-[UIImageView:0x7f87650162c0]   (active, names: '|':Testing.MyTableView:0x7f8765010b70 )>",
    "<NSLayoutConstraint:0x60400008e510 UITableView:0x7f8763865400.height == UIImageView:0x7f87650162c0.height - 44   (active)>",
    "<NSLayoutConstraint:0x608000097110 '_UITemporaryLayoutHeight' Testing.MyTableView:0x7f8765010b70.height == 0   (active)>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x6080000944b0 UIImageView:0x7f87650162c0.bottom == Testing.MyTableView:0x7f8765010b70.bottom   (active)>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.

Anybody have an idea how to fix that?
Thank you in advance.

@murselturk It has nothing wrong with Align, it’s just usual autolayout conflict. Autolayout can’t resolve equations you’ve given. You’ve basically said something like “calculate my bgView’s height based on root view, and make my tableView’s height less than bgView’s”. But autolayout calculates sizes from deepest view in hierarchy to the most top and it has no idea, how exactly it should implement your requirement, because when it tries to calculate tableView, bgView is not calculated yet. You have to provide more information about positioning. Try to set tableView’s height constraint priority to lower value. Or even completely rethink your constraints. Like,
•view
••bgView{edges to superview}
•••searchBar{top-left-right to superview, height 44}
•••tableView{bottom-left-right to superview, top to searchBar’s bottom}
This should work.

And you need to read about constraint’s priority and view’s resistance and hugging priorities to avoid that kind of mistakes in future.

@ivanpetrov16130 Thanks. I've tried also with the following code:

addSubview(bgView) {
    $0.edges.pinToSuperview()
}

addSubview(searchBar) {
    $0.edges(.top, .left, .right).pinToSuperview()
    $0.height.set(44)
}

addSubview(tableView) {
    $0.edges(.bottom, .left, .right).pinToSuperview()
    $0.top.align(with: searchBar.al.bottom)
}

But same result.

@ivanpetrov16130 Constraint priority solved my problem. Thanks again.

addSubview(bgView) {
    $0.edges.pinToSuperview()
}

addSubview(searchBar, tableView) {
    $1.top.align(with: $0.bottom)

    $0.edges(.top, .left, .right).pinToSuperview()
    $0.height.set(44)

    $1.edges(.left, .right).pinToSuperview()
    $1.bottom.pinToSuperview().priority = UILayoutPriority(999)
}