UILabel在UITtackView中的UITableViewCell中,numberOfLines = 0

时间:2017-11-29 15:15:49

标签: ios uitableview uilabel nslayoutconstraint uistackview

我在UIStackView中有一个UITableViewCell,带有UIImageView和UILabel。 我希望UIStackView的发行版能够按比例填充。

  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
  }

当我为UILabel设置numberOfLines而不是1时,我得到了这个约束警告:

[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)>

实际上,我得到了我想要的结果。

enter image description here

但是,有没有办法解决这个约束警告?

0 个答案:

没有答案