0

我对 UICollection 视图列表的 separatorLayoutGuide 有疑问。我看到了这篇文章,明白我需要重写函数 updateConstraints() 以更新分隔符布局指南。像这样...

override func updateConstraints() {
  super.updateConstraints()

  separatorLayoutGuide.leadingAnchor.constraint(equalTo: otherView.leadingAnchor, constant: 0.0).isActive = true
}

我可以看到单元格的前导锚和分离向导的前导锚之间的微小空间,如下图所示,我想修复它。(像单元格的左侧) 在此处输入图像描述

但是,问题是,我使用本文创建了一个自定义集合视图列表单元格,并且无法更改导致自定义视图前导的 separatorLayoutGuide。我添加了customListCell.separatorLayoutGuide.leadingAnchor.constraint(equalTo: self.leadingAnchor).isActive = true为了将 separatorLayoutGuide 的前导定位到 customView 的前导,我得到了

"UILayoutGuide:0x2822d8b60'UICollectionViewListCellSeparatorLayoutGuide'.leading"> and <NSLayoutXAxisAnchor:0x280e9cac0 "ContentView:0x15960db90.leading"> because they have no common ancestor.  Does the constraint or its anchors reference items in different view hierarchies?  That's illegal.'

错误。完成研究后,我想我没有为 separatorLayoutGuide 添加子视图,但即使我向自定义视图添加子视图,应用程序也会崩溃。使用自定义 UIView 时,有没有办法更改分隔符指南的前导锚点?

class CustomListCell: UICollectionViewListCell {

    var item: TestItem?
    
    override func updateConfiguration(using state: UICellConfigurationState) {
        
        // Create new configuration object
        var newConfiguration = ContentConfiguration().updated(for: state)
        
            newConfiguration.name = item.name
            newConfiguration.state = item.state

        // Set content configuration
        contentConfiguration = newConfiguration
    }
}


struct ContentConfiguration: UIContentConfiguration, Hashable {
    
    var name: String?
    var state: String?
    
    func makeContentView() -> UIView & UIContentView {
        return ContentView(configuration: self)
    }
    
    func updated(for state: UIConfigurationState) -> Self {
        guard let state = state as? UICellConfigurationState else {
            return self
        }
        
        // Updater self based on the current state
        let updatedConfiguration = self
        if state.isSelected {
            print("is selected")
        } else {
            print("is deselected")
        }
        return updatedConfiguration
    }
}


class ContentView: UIView, UIContentView {

    let contentsView = UIView()
    let customListCell = CustomListCell()

    lazy var titleLabel: UILabel = {
        let label = UILabel()
        label.text = ""
        return label
    }()

    lazy var statusLabel: UILabel = {
        let label = UILabel()
        label.text = ""
        return label
    }()

    lazy var symbolImageView: UIImageView = {
        let imageView = UIImageView()
        imageView.contentMode = .scaleAspectFit
        return imageView
    }()

    init(configuration: ContentConfiguration) {
          // Custom initializer implementation here.
        super.init(frame: .zero)
        setupAllViews()
        apply(configuration: configuration)
    }


    override func updateConstraints() {
        super.updateConstraints()
        customListCell.separatorLayoutGuide.leadingAnchor.constraint(equalTo: self.leadingAnchor).isActive = true
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    private var currentConfiguration: ContentConfiguration!
    var configuration: UIContentConfiguration {
        get {
            currentConfiguration
        }
        set {
            guard let newConfiguration = newValue as? ContentConfiguration else {
                return
            }

            apply(configuration: newConfiguration)
        }
    }

    func setupAllViews() {
        // add subviews and add constraints
    }

    func apply(configuration: ContentConfiguration) {

    }
}
4

1 回答 1

0

通过覆盖UICollectionViewListCell子类中的 updateConstraints

于 2021-12-17T06:05:49.297 回答