Search code examples
swiftanchorcollectionview

How to make whole collectionView visible?


I'm using a collectionview in my project but it's covered by some other view as the imageenter image description here

Because I'm anchoring the views in the view I don't know how to proceed. is there any function I should call? I'm using my own anchoring function btw

view.addSubview(projectHashtagInputView)
selectImageView.addSubview(collectionView)

collectionView.anchor(top: selectImageView.chooseImagesButton.bottomAnchor, left: selectImageView.leftAnchor, bottom: nil, right: selectImageView.rightAnchor, paddingTop: 2, paddingLeft: 2, paddingBottom: 2, paddingRight: 2, width: 0, height: 75)

selectImageView.anchor(top: projectHashtagInputView.bottomAnchor, left: view.safeAreaLayoutGuide.leftAnchor, bottom: collectionView.bottomAnchor, right: view.rightAnchor, paddingTop: 4, paddingLeft: 12, paddingBottom: -10, paddingRight: rightPadding, width: 0, height: 0)

How do I show the whole collectionView?


Solution

  • Override UICollectionView and implement it like this

    class CollectionView: UICollectionView {
    
      override var contentSize: CGSize {
        didSet {
          invalidateIntrinsicContentSize()
        }
      }
    
      override var intrinsicContentSize: CGSize {
        return CGSize(width: max(10, contentSize.width), height: max(10, height)
      }
    }
    

    Set that class as the class for your collection view. Now, if you don't add a height constraint, the collection view will expand to show all it's content.