0

I have tableViewCell, and it have UIButton. I set up constraints like that:

make.left.equalTo(contentView.snp.left).offset(16)
                make.right.equalTo(contentView.snp.right).offset(-16)
                make.bottom.equalTo(contentView.snp.bottom).offset(-46)
                make.top.equalTo(contentView.snp.top).offset(24)
                make.height.equalTo(64)

It does show button with correct height and set top and bottom offsets correct, but it print error log in console:

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:0x6000002a34e0@RoundedButtonCell.swift#49 POS2.MSButton:0x7faf22c674b0.bottom == UITableViewCellContentView:0x7faf22c672c0.bottom - 46.0>",
    "<SnapKit.LayoutConstraint:0x6000000b8600@RoundedButtonCell.swift#50 POS2.MSButton:0x7faf22c674b0.top == UITableViewCellContentView:0x7faf22c672c0.top + 24.0>",
    "<SnapKit.LayoutConstraint:0x6000002a3de0@RoundedButtonCell.swift#51 POS2.MSButton:0x7faf22c674b0.height == 64.0>",
    "<NSLayoutConstraint:0x604000484420 'UIView-Encapsulated-Layout-Height' UITableViewCellContentView:0x7faf22c672c0.height == 134   (active)>"
)

Will attempt to recover by breaking constraint 
<SnapKit.LayoutConstraint:0x6000002a3de0@RoundedButtonCell.swift#51 POS2.MSButton:0x7faf22c674b0.height == 64.0>

I suppose it somehow "dont like" that i set UIButton height with constant, and set top and bottom offsets. How to fix that?

PS. My controller is UITableViewController, i configure tableView like that:

private func setupTable() {
        tableView.estimatedRowHeight = 40
        tableView.rowHeight = UITableViewAutomaticDimension

        tableView.separatorInset = .zero
        tableView.separatorColor = .clear

        tableView.keyboardDismissMode = .onDrag

        tableView.tableFooterView = nil
        tableView.tableHeaderView = nil

        tableView.delegate = self
        tableView.dataSource = self

        tableView.register(RadioButtonCell.self, forCellReuseIdentifier: MSTableViewCellType.radioButtonCell.rawValue)
        tableView.register(InputViewCell.self, forCellReuseIdentifier: MSTableViewCellType.inputViewCell.rawValue)
        tableView.register(CommentViewCell.self, forCellReuseIdentifier: MSTableViewCellType.commentViewCell.rawValue)
        tableView.register(RoundedButtonCell.self, forCellReuseIdentifier: MSTableViewCellType.roundedButtonViewCell.rawValue)
    }

setupTable is called in viewDidLoad

In cellForRow:

  case .roundedButton:
            let cell = tableView.dequeueReusableCell(withIdentifier: MSTableViewCellType.roundedButtonViewCell.rawValue, for: indexPath) as! RoundedButtonCell
            guard let model = fieldModel as? TextContainViewModel else { return UITableViewCell()}
            cell.setup(title: model.title)
            cell.contentView.layoutMargins = UIEdgeInsets(top: 24, left: 16, bottom: 46, right: 16)
            cell.layoutIfNeeded()

            return cell
        }

I did not override heightForRowAtIndexPath.

Evgeniy Kleban
  • 6,794
  • 13
  • 54
  • 107
  • Probable duplicate of e.g. https://stackoverflow.com/questions/30299319/why-uitableviewautomaticdimension-not-working – matt Dec 26 '17 at 19:07

1 Answers1

1

The problem is that you are already setting the cell (row) height explicitly. You cannot dictate the row height explicitly and also provide a complete set of full priority vertical constraints. Do one or the other. Either size the row explicitly and provide incomplete constraints, or provide complete constraints and tell the row to size itself automatically.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • how can i provide complete constraints ? I want my view to be fixed height and i need both top and bottom offset. – Evgeniy Kleban Dec 26 '17 at 17:01
  • You want _what_ view to be fixed height? The button or the cell (row)? Pick _one_. That is what my answer tells you. Try just leaving out the line `make.height.equalTo(64)`. What happens? – matt Dec 26 '17 at 17:20
  • I don’t want cell to be fixed height I want it to be top offset + view height + bot offset height. Please help me to modify my code to fix warning – Evgeniy Kleban Dec 26 '17 at 18:35
  • and I don’t want to set cell height explicit. If I do, I don’t mean to. Please tell me how to make cell height dynamic – Evgeniy Kleban Dec 26 '17 at 18:36
  • Set the table view's estimated row height to something (e.g. 140) and set the table view's actual row height to UITableViewAutomaticDimension. You can do that for the whole table view, or you can do it on a per-row basis with `heightForRowAt`. – matt Dec 26 '17 at 18:54
  • Then show it. Show enough that I can _reproduce_ the problem. I don't know what you're doing if you don't show it. I assure you that what I have said does work. I could post an example to prove it. But it would be better for you to ask the question more fully and informatively. – matt Dec 26 '17 at 19:03
  • Great but this is still not enough to reproduce the issue. I'd need to see every single constraint that you set, by any means whatever, in the context where it occurs, plus your implementation of `heightForRowAt` if there is one. – matt Dec 26 '17 at 19:40
  • could i ask you a question - if that perfectly ok to set constraints in view, that is inside of superview (superview is view that have no strong height constraints) like : vertical offset to superview + fixedViewHeight + bottom offset to superver + left and right constraints? – Evgeniy Kleban Dec 26 '17 at 19:47
  • Certainly it's okay in general. - I'm still not getting enough info to track down the issue. I suggest you run your app and switch in Xcode to the View Debugger. It will show you your constraints and the conflicts and you'll be able to see the issue more clearly. – matt Dec 26 '17 at 19:54
  • problem is only occure on iPhone Plus. In other models there is no error in log. – Evgeniy Kleban Dec 26 '17 at 19:54