0

In my UITableViewCell subclass I'm adding two elements, and I specify them as shown below. Note how I'm leaving off the bottomConstraint on the ball. When I do that, Xcode give a runtime warning saying height couldn't be determined and it's guessing for me.

override func awakeFromNib() {
    super.awakeFromNib()

    contentView.addSubview(date)   // Date is a UILabel
    contentView.addSubview(balls)  // Balls is a UIStackView subclass

    balls.ballSize = 35  // This adds a height 35 constraint to balls

    NSLayoutConstraint.activate([
        date.leadingAnchor.constraint(equalTo: safeAreaLayoutGuide.leadingAnchor, constant: 20),
        date.centerYAnchor.constraint(equalTo: safeAreaLayoutGuide.centerYAnchor)
    ])

    regularConstraints = [
        date.trailingAnchor.constraint(greaterThanOrEqualTo: balls.leadingAnchor, constant: 20),
        balls.topAnchor.constraint(equalTo: safeAreaLayoutGuide.topAnchor, constant: 10),
        //balls.bottomAnchor.constraint(equalTo: safeAreaLayoutGuide.bottomAnchor, constant: -10),
        balls.trailingAnchor.constraint(equalTo: safeAreaLayoutGuide.trailingAnchor, constant: -20),
    ]

    NSLayoutConstraint.activate(regularConstraints)
}

So then I uncomment the line for the bottom anchor, which means I've now fully constrained the height of the cell. But when I do that, I get all this garbage. I have no idea why it's having an issue:

(

"<NSLayoutConstraint:0x6040002896f0 DidIWin.BallView:0x7f81c8d51af0.width == 35   (active)>",
"<NSLayoutConstraint:0x604000289880 DidIWin.BallView:0x7f81c8d51af0.height == DidIWin.BallView:0x7f81c8d51af0.width   (active)>",
"<NSLayoutConstraint:0x60400028aaf0 DidIWin.Balls:0x7f81c8d4fd70.top == UILayoutGuide:0x6040001b1720'UIViewSafeAreaLayoutGuide'.top + 10   (active)>",
"<NSLayoutConstraint:0x60400028ab40 DidIWin.Balls:0x7f81c8d4fd70.bottom == UILayoutGuide:0x6040001b1720'UIViewSafeAreaLayoutGuide'.bottom - 10   (active)>",
"<NSLayoutConstraint:0x60400028a190 'UISV-alignment' DidIWin.BallView:0x7f81c8d500d0.bottom == DidIWin.BallView:0x7f81c8d51af0.bottom   (active)>",
"<NSLayoutConstraint:0x60400028a370 'UISV-alignment' DidIWin.BallView:0x7f81c8d500d0.top == DidIWin.BallView:0x7f81c8d51af0.top   (active)>",
"<NSLayoutConstraint:0x604000288390 'UISV-canvas-connection' DidIWin.Balls:0x7f81c8d4fd70.top == DidIWin.BallView:0x7f81c8d500d0.top   (active)>",
"<NSLayoutConstraint:0x60400028a0a0 'UISV-canvas-connection' V:[DidIWin.BallView:0x7f81c8d500d0]-(0)-|   (active, names: '|':DidIWin.Balls:0x7f81c8d4fd70 )>",
"<NSLayoutConstraint:0x60400028a2d0 'UIView-Encapsulated-Layout-Height' DidIWin.FailureDetailCell:0x7f81ca0d5800'failure'.height == 44   (active)>",
"<NSLayoutConstraint:0x604000289ce0 'UIViewSafeAreaLayoutGuide-bottom' V:[UILayoutGuide:0x6040001b1720'UIViewSafeAreaLayoutGuide']-(0)-|   (active, names: '|':DidIWin.FailureDetailCell:0x7f81ca0d5800'failure' )>",
"<NSLayoutConstraint:0x604000289c40 'UIViewSafeAreaLayoutGuide-top' V:|-(0)-[UILayoutGuide:0x6040001b1720'UIViewSafeAreaLayoutGuide']   (active, names: '|':DidIWin.FailureDetailCell:0x7f81ca0d5800'failure' )>"

)

Will attempt to recover by breaking constraint

NSLayoutConstraint:0x604000289880 DidIWin.BallView:0x7f81c8d51af0.height == DidIWin.BallView:0x7f81c8d51af0.width (active)>

The Balls stackview contains 6 BallView classes, and those have a height==width constraint on them.

Community
  • 1
  • 1
Gargoyle
  • 9,590
  • 16
  • 80
  • 145
  • Why do you pin constraints to the `safeAreaLayoutGuide` within the `UITableViewCell`? You should ensure that the table view, not the cell, won't go beyond the safe area. Try pin these constraints to the superview instead? – Kamil Szostakowski Nov 08 '17 at 20:18
  • @KamilSzostakowski That's a very good question. Habit I guess. I switched to using the visual constraints and now it's working correctly. I'm going to guess it did it the way you suggested. Thanks. I'll remember this for the future though. – Gargoyle Nov 08 '17 at 21:54
  • I think it's prudent to disable this feature in .xibs which contain only custom controls. It's kind of a view controller responsibility to deal with them. – Kamil Szostakowski Nov 08 '17 at 22:09

0 Answers0