Search code examples
iosswiftselectioncollectionviewdisable

How can I disable Collection View but Header left enbled?


I need to disallow Collection View selection, but left CV's header enabled?

Both methods block header. How to enable it?

CollectionView.isUserInteractionEnabled = false

or

CollectionView.allowsSelection = false

Here is 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),
        ])
    }
}

This is horizontal CV embedded in other vertical CV's header. I need to disable vertical CV but left enabled horizontal CV in vertical CV' header )


Solution

  • To block I used CollectionView.isUserInteractionEnabled = false and it blocks everything even header.

    I tried CollectionView.allowsSelection = false and it blocks CV and doesn't block header!

    It works.

    Before I just didn't pay attention that .allowSelection doesn't block header .