代码之家  ›  专栏  ›  技术社区  ›  Ogün Birinci

重新加载数据后出现UICollectionViewCell约束错误

  •  0
  • Ogün Birinci  · 技术社区  · 3 年前

    这是我的自定义UICollectionViewCell代码。我只是在configureViews()函数中为子视图提供了一个约束。

    class CategoryCell: UICollectionViewCell {
      private lazy var nameLabel: UILabel = {
        let label = PaddingLabel()
        label.numberOfLines = 1
        label.adjustsFontForContentSizeCategory = true
        label.font = UIFont.boldSystemFont(ofSize: UIFont.preferredFont(forTextStyle: .subheadline).pointSize)
        label.textAlignment = .center
        let widthConstant = floor((UIScreen.main.bounds.size.width) / 3)
        label.widthAnchor.constraint(equalToConstant: widthConstant).isActive = true
        return label
      }()
    
      private lazy var bottomLine = UIView()
    
      override init(frame: CGRect) {
        super.init(frame: frame)
        configureViews()
      }
    
      required init?(coder: NSCoder) {
        super.init(coder: coder)
        configureViews()
      }
    
      override func prepareForReuse() {
        super.prepareForReuse()
        nameLabel.text = nil
      }
    }
    
    private extension CategoryCell {
      func configureViews() {
        contentView.addSameSizeConstrained(subView: nameLabel)
        contentView.addSubview(bottomLine)
    
        bottomLine.translatesAutoresizingMaskIntoConstraints = false
    
        NSLayoutConstraint.activate([
          bottomLine.leadingAnchor.constraint(equalTo: contentView.leadingAnchor),
          bottomLine.topAnchor.constraint(equalTo: contentView.bottomAnchor),
          bottomLine.trailingAnchor.constraint(equalTo: contentView.trailingAnchor),
          bottomLine.heightAnchor.constraint(equalToConstant: AppLayoutGuidance.spacingHalf / 4),
        ])
      }
    }
    

    当第一次渲染单元格时,没有问题,但当我调用collectionView.reloadData()函数时,控制台上会出现一个约束错误。你知道吗?

    编辑:约束控制台日志

    "<NSLayoutConstraint:0x600000d01090 App.PaddingLabel:0x13d494320.width == 130   (active)>",
    "<NSLayoutConstraint:0x600000d010e0 H:|-(0)-[App.PaddingLabel:0x13d494320]   (active, names: '|':UIView:0x13d467360 )>",
    "<NSLayoutConstraint:0x600000d01130 App.PaddingLabel:0x13d494320.trailing == UIView:0x13d467360.trailing   (active)>",
    "<NSLayoutConstraint:0x600000d02850 'UIView-Encapsulated-Layout-Width' UIView:0x13d467360.width == 0   (active)>"
    
    0 回复  |  直到 3 年前
        1
  •  0
  •   Omer Tekbiyik user11084389    3 年前

    我认为addSameSizeConstrainted函数,顾名思义,使添加了contentview的视图的框架相同。所以 width nameLabel 等于 contentView

    但在定义 label ,您给标签的宽度值为 floor((UIScreen.main.bounds.size.width) / 3)

    UIView-Encapsulated-Layout-Width' UIView:0x13d467360.width == 0 这个信息基本上就是给我们提供信息。你必须付出 priority 其中一个约束条件是:

    • 声明中的nameLabel宽度约束= 地板((UIScreen.main.bounds.size.width)/3)
    • name相等内容中的标签宽度约束查看中的宽度 addSameSizeConstrained

    可以通过优先考虑以下约束之一来解决此问题:

    let widthConstant = floor((UIScreen.main.bounds.size.width) / 3)
    let const = label.widthAnchor.constraint(equalToConstant: widthConstant)
       const.priority = .defaultLow
       const.isActive = true
    return label
    

    您第一次加载时没有在控制台中收到此警告,因为您的集合视图的估计大小必须为 Automatic .如果您更改 None ,您将在第一次加载时看到此警告。