【问题标题】:Access view file from UICollectionViewListCell从 UICollectionViewListCell 访问视图文件
【发布时间】:2022-01-08 19:21:04
【问题描述】:

我对 UICollection 视图列表的 separatorLayoutGuide 有疑问。我看到 this article 并明白我需要重写函数 updateConstraints() 以更新分隔符布局指南。 像这样……

override func updateConstraints() {
  super.updateConstraints()

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

我可以看到单元格的前导锚点和 seprateguide 的前导锚点之间的微小空间,如下图所示,我想修复它。 (像单元格的左侧)

但是,问题是,我使用 this article 创建了一个自定义集合视图列表单元格,并且无法更改导致自定义视图前导的 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) {

    }
}

【问题讨论】:

    标签: ios swift collectionview separator uicollectionviewlistcell


    【解决方案1】:

    通过覆盖 UICollectionViewListCell 子类中的 updateConstraints

    【讨论】:

    • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-03-06
    • 1970-01-01
    • 1970-01-01
    • 2017-05-16
    • 1970-01-01
    • 2011-07-07
    相关资源
    最近更新 更多