2

I have a UITableViewCell, with an UIImageView and a UILabel, inside a UIStackView. I want the distribution for the UIStackView to fillProportionally.

  override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let iconView = UIImageView(image: #imageLiteral(resourceName: "Regular Folder"))
    iconView.contentMode = .scaleAspectFit

    let label = UILabel()
    label.numberOfLines = 0 // or 2, or 3...
    label.text = "a Long Text"

    let stackView = UIStackView(arrangedSubviews: [iconView, label])
    stackView.axis = .horizontal
    stackView.alignment = .fill
    stackView.distribution = .fillProportionally

    let cell = UITableViewCell(style: .default, reuseIdentifier: nil)
    cell.contentView.addSubview(stackView)
    stackView.translatesAutoresizingMaskIntoConstraints = false

    stackView.leadingAnchor.constraint(equalTo: cell.contentView.leadingAnchor).isActive = true
    stackView.topAnchor.constraint(equalTo: cell.contentView.topAnchor).isActive = true
    cell.contentView.trailingAnchor.constraint(equalTo: stackView.trailingAnchor).isActive = true
    cell.contentView.bottomAnchor.constraint(equalTo: stackView.bottomAnchor).isActive = true

    return cell
  }

When I set numberOfLines for the UILabel to anything but 1, I got this constraint warning:

[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:0x1c409d0b0 H:|-(0)-[UIStackView:0x101507370]   (active, names: '|':UITableViewCellContentView:0x10150f9f0 )>",
    "<NSLayoutConstraint:0x1c409d1f0 H:[UIStackView:0x101507370]-(0)-|   (active, names: '|':UITableViewCellContentView:0x10150f9f0 )>",
    "<NSLayoutConstraint:0x1c0099d20 'fittingSizeHTarget' UITableViewCellContentView:0x10150f9f0.width == 320   (active)>",
    "<NSLayoutConstraint:0x1c0098240 'UISV-canvas-connection' UIStackView:0x101507370.leading == UIImageView:0x10160ac40.leading   (active)>",
    "<NSLayoutConstraint:0x1c00981f0 'UISV-canvas-connection' H:[UILabel:0x101505700'Label']-(0)-|   (active, names: '|':UIStackView:0x101507370 )>",
    "<NSLayoutConstraint:0x1c0099870 'UISV-fill-proportionally' UIImageView:0x10160ac40.width == UIStackView:0x101507370.width   (active)>",
    "<NSLayoutConstraint:0x1c0099b40 'UISV-fill-proportionally' UILabel:0x101505700'Label'.width == UIStackView:0x101507370.width   (active)>",
    "<NSLayoutConstraint:0x1c00999b0 'UISV-spacing' H:[UIImageView:0x10160ac40]-(0)-[UILabel:0x101505700'Label']   (active)>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x1c00999b0 'UISV-spacing' H:[UIImageView:0x10160ac40]-(0)-[UILabel:0x101505700'Label']   (active)>

So actually, I'm getting the result I want.

enter image description here

But, is there a way to fix this constraint warning ?

nouatzi
  • 735
  • 4
  • 14
  • are you using `UITableViewDynamicDimension` for the row height? – Milan Nosáľ Nov 29 '17 at 15:21
  • I've tried tableView.rowHeight = UITableViewAutomaticDimension or tableView.rowHeight = 44.0 I still got the warning... – nouatzi Nov 29 '17 at 15:29
  • try to change the values of the label: hugging priority, and the second one (can't remember it's name right now) – Yitzchak Nov 29 '17 at 15:32
  • I've tried `setContentHuggingPriority` and `setContentCompressionResistancePriority` on both label and imageview, but still, nothing changes with this warning. – nouatzi Nov 29 '17 at 15:39
  • @nouatzi - what is the end result you are trying to get? can you show an image of how you want it to look? – DonMag Nov 29 '17 at 16:20
  • I've added the result image. So actually I'm getting what I want, but if I could get rid of this constraint warning. – nouatzi Nov 29 '17 at 16:48
  • 1
    @nouatzi - well, let me throw this out there... StackViews are great - except when they're not. If your cell consists solely of an Image and a multi-line label (I'm assuming it will be of varying heights, depending on the text), I think I'd recommend ***not*** using a StackView. It would be a piece of cake to get that cell layout working *without* a StackView. – DonMag Nov 29 '17 at 20:51
  • @DonMag I agree with you – Yitzchak Nov 29 '17 at 21:37
  • Unfortunately I've shown you a simplified version of my cell. Actually it has 2 images, 4 labels, and 1 activityIndicator. – nouatzi Nov 30 '17 at 04:14
  • It seems like stackview doesn't like to be 'stack' with other than a stackview. I've put the label in another stackview, and the image in another stackview: `let iconStackView = UIStackView(arrangedSubviews: [iconView]); let labelStackView = UIStackView(arrangedSubviews: [label]); let stackView = UIStackView(arrangedSubviews: [iconStackView, labelStackView])` And the constraint warning has gone. And I'm still getting the expected arrangement. – nouatzi Nov 30 '17 at 04:21
  • @nouatzi - tip for the future: Don't ask a question that is not really your question. It just wastes people's time. – DonMag Nov 30 '17 at 13:35

0 Answers0