skip to Main Content

I want to move the height to fit the contents inside. This CollectionView after every cell has been rendered. this is screen app -> enter image description here

I want to move the height to fit the contents inside. This CollectionView after every cell has been rendered.

2

Answers


  1. To dynamically adjust the height of your CollectionView to accommodate the content within each cell after rendering, consider integrating the "DynamicHeightCollectionView" pod. This pod, has proven to work seamlessly in similar scenarios.

    To incorporate the pod into your project, follow these steps:

    1. Add the pod to your project by including the following line in your
      Podfile:

      pod ‘DynamicHeightCollectionView’

    2. Run pod install in your terminal to install the new dependency.

    3. Utilize the DynamicHeightCollectionView class for your collection
      view:

      let dynamicHeightCollectionView = DynamicHeightCollectionView()
      // Configure your collection view as needed
      // …

    By leveraging this pod, you should experience improved flexibility in adjusting the height of your CollectionView based on the content within each rendered cell. Ensure to consult the documentation provided by the pod for any additional customization options.

    Login or Signup to reply.
  2. I think you need to check initially if collection content height is bigger then collection frame height.

    Here is an example using frame, you can also do it with NSLayoutConstraint

    if collectionView.contentSize.height > collectionView.frame.size.height {
        // handle the case when content height i bigger then frame height
    } else {
        // handle the case when content height is less then frame height
        var frame = collectionView.frame
        frame.size.height = collectionView.contentSize.height
        collectionView.frame = frame
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search