1

I have a tableview and custom cell that loads from nib file, this cell has a height constraint (80.0 in my case) and when I launch the app Xcode says that there is a constraint error. I found that this problem occur only if a tableview has separators. Is there a way to solve this constraint error if I want to have both height constraint of the cell, and separators in tableview?

ViewController with tableview

class ViewController: UIViewController, UITableViewDataSource {

    @IBOutlet weak var tableView: UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.register(UINib(nibName: "SampleTableViewCell", bundle: nil), forCellReuseIdentifier: "SampleTableViewCell")
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "SampleTableViewCell", for: indexPath) as! SampleTableViewCell
        return cell
    }

}

Error I get

2019-10-13 18:08:04.345180+0300 tableViewProblem[5281:391449] [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:0x600000ca8320 UIView:0x7f988cf0e840.height == 80   (active)>",
    "<NSLayoutConstraint:0x600000cb1400 V:[UIView:0x7f988cf0e840]-(0)-|   (active, names: '|':UITableViewCellContentView:0x7f988cf0e420 )>",
    "<NSLayoutConstraint:0x600000cb14a0 V:|-(0)-[UIView:0x7f988cf0e840]   (active, names: '|':UITableViewCellContentView:0x7f988cf0e420 )>",
    "<NSLayoutConstraint:0x600000cbcdc0 'UIView-Encapsulated-Layout-Height' UITableViewCellContentView:0x7f988cf0e420.height == 80.5   (active)>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x600000ca8320 UIView:0x7f988cf0e840.height == 80   (active)>

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

My custom tableview cell

How my app looks

Kostya38
  • 11
  • 2
  • For issues like this https://www.wtfautolayout.com is a really great tool – Bartosz Kunat Oct 13 '19 at 16:41
  • It looks like I found the solution here: https://stackoverflow.com/a/32283863/7058609 I changed the priority parameter of height constraint to 999 (it was 1000) and no more constraint errors – Kostya38 Oct 13 '19 at 17:36

0 Answers0