【问题标题】:How can I disable Collection View but Header left enbled?如何禁用 Collectionview 但 Header 保持启用?
【发布时间】:2022-06-15 01:48:27
【问题描述】:

我需要禁止选择集合视图,但启用了 CV 的标题?

这两种方法都阻塞了头部。如何开启?

CollectionView.isUserInteractionEnabled = false

CollectionView.allowsSelection = false

这里是 UICollectionReusableView:

final class HeaderCollectionReusableView: UICollectionReusableView {
    
    override init(frame: CGRect) {
        super.init(frame: .zero)
        
        addSubView()
        setupLayout()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    static let headerIdentifier = "HeaderCollectionReusableView"
    
    private func addSubView() {
        
        addSubview(collectionView)
    }
    
    public lazy var collectionView: UICollectionView = {
        
        var flowLayout = UICollectionViewFlowLayout()
        flowLayout.scrollDirection = .horizontal
        flowLayout.itemSize = CGSize(width:  UIScreen.main.bounds.size.width / 3.5, height: UIScreen.main.bounds.size.width / 3)
        flowLayout.minimumLineSpacing = 12
        flowLayout.sectionInset = UIEdgeInsets(top: 0, left: 20, bottom: 0, right: 20)
        var view = UICollectionView(frame: .zero, collectionViewLayout: flowLayout)
        view.register(cell: CollectionViewCell.self)
        view.backgroundColor = .clear
        view.showsHorizontalScrollIndicator = false
        view.showsVerticalScrollIndicator = false
        view.contentInsetAdjustmentBehavior = .automatic
        view.translatesAutoresizingMaskIntoConstraints = false
        return view
    }()
    
    private func setupLayout() {
        
        NSLayoutConstraint.activate([
            collectionView.topAnchor.constraint(equalTo: topAnchor),
            collectionView.leadingAnchor.constraint(equalTo: leadingAnchor),
            collectionView.trailingAnchor.constraint(equalTo: trailingAnchor),
            collectionView.heightAnchor.constraint(equalToConstant: UIScreen.main.bounds.size.width / 3),
        ])
    }
}

这是嵌入在其他垂直 CV 标题中的水平 CV。 我需要禁用垂直 CV,但在垂直 CV' 标题中启用水平 CV)

【问题讨论】:

  • 请分享您的标题视图代码。
  • @Asteroid 代码共享)
  • 您不能禁用集合视图上的用户交互并仅为您有权访问的标题视图启用它吗? developer.apple.com/documentation/uikit/uitableview/…
  • @cora I cant ) 我禁用了 CV 用户交互,并且它的标题也禁用了。
  • 您是否尝试为每个单元格禁用(在单元格 For Row 中)?

标签: ios swift selection collectionview disable


【解决方案1】:

我曾经阻止 CollectionView.isUserInteractionEnabled = false 并且它阻止了所有内容,甚至是标题。 我深入尝试了 CollectionView.allowsSelection = false 并且它会阻止 CV 并且不会阻止标题!有用。 )

之前我只是没有注意到 .allowSelection 不会阻止标题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-06
    相关资源
    最近更新 更多