iamhx iamhx - 2 years ago 269
Swift Question

Unable to simultaneously satisfy constraints Custom Header Section

I have a .nib for my custom header section for a table view:

enter image description here

override func viewDidLoad() {
super.viewDidLoad()

let nib = UINib(nibName: "BillTableSection", bundle: nil)
billTableView.register(nib, forHeaderFooterViewReuseIdentifier: "BillTableSection")

}

func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {

return 44.0
}

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {

let cell = billTableView.dequeueReusableHeaderFooterView(withIdentifier: "BillTableSection")

return cell
}


The custom header section works, but whenever I insert a new section, A warning shows up on my console:

2017-10-02 14:56:32.791529+0800 Project[1017:14993] [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:0x61000009f8b0 UIButton:0x7ff359d0d4a0.width == 22 (active)>",
"<NSLayoutConstraint:0x61000009f900 UITextField:0x7ff359d19660.width == UILabel:0x7ff359d17650'$0.00'.width (active)>",
"<NSLayoutConstraint:0x61000009fa90 UITextField:0x7ff359d19660.leading == UIView:0x7ff359d010a0.leadingMargin (active)>",
"<NSLayoutConstraint:0x61000009ed20 H:[UITextField:0x7ff359d19660]-(30)-[UILabel:0x7ff359d17650'$0.00'] (active)>",
"<NSLayoutConstraint:0x61000009f2c0 UIButton:0x7ff359d0d4a0.trailing == UIView:0x7ff359d010a0.trailingMargin (active)>",
"<NSLayoutConstraint:0x61000009ee60 H:[UILabel:0x7ff359d17650'$0.00']-(7)-[UIButton:0x7ff359d0d4a0] (active)>",
"<NSLayoutConstraint:0x61000009f5e0 H:[UIView:0x7ff359d010a0]-(0)-| (active, names: '|':Project.BillTableSection:0x7ff359d0fc70 )>",
"<NSLayoutConstraint:0x61000009f9a0 H:|-(0)-[UIView:0x7ff359d010a0] (active, names: '|':Project.BillTableSection:0x7ff359d0fc70 )>",
"<NSLayoutConstraint:0x61000009ff40 '_UITemporaryLayoutWidth' Project.BillTableSection:0x7ff359d0fc70.width == 0 (active)>"
)

Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x61000009f8b0 UIButton:0x7ff359d0d4a0.width == 22 (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.


I tried to find the constraints that are not needed and remove them, but the layout I defined will not work on other devices anymore. Is it safe to ignore this warning? I'm not sure how to debug this... Please help!

Answer Source

You should lower the BtnAdd.trailing priority to less than 1000. it looks like autolayout adds a temporary constraint

"<NSLayoutConstraint:0x61000009ff40 '_UITemporaryLayoutWidth' Project.BillTableSection:0x7ff359d0fc70.width == 0 (active)>"

that squishes all the content, so removing that absolute priority will let your content adjust without conflicts.


Also, why is TxtName.width == LblPrice.width? Can't you adjust content hugging priority?

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download