6
votes

I have one collectionview and a cell inside that which is having a fixed width and dynamic height based on its contents.I am using autolayout constraints , my code is below.

I have already tried to set all types of auto constraints inside the cell and inside the view .

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
            return 5
        }

        func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! ViewControllerCell

            cell.lbl_text.text = "djs sdfs sdfs dsfsfd gdgfd dfgd df "

            return cell

        }



        override func viewDidLoad() {
            super.viewDidLoad()
            // Do any additional setup after loading the view.

            if let flowLayout = coll_main?.collectionViewLayout as? UICollectionViewFlowLayout {
                flowLayout.estimatedItemSize = UICollectionViewFlowLayout.automaticSize

            }



    }

In cell class method:
override func awakeFromNib() {
        super.awakeFromNib()
        self.contentView.autoresizingMask = [UIView.AutoresizingMask.flexibleHeight]
    }

    override func layoutSubviews() {
        self.layoutIfNeeded()
    }

i get cell width fix without i am set that width. how may this issue solve?

here screenshots.

enter image description here enter image description here

6
You have to implement sizeForItemAt to provide the correct size.Kamran

6 Answers

9
votes

You need to make sure that your Estimate Size is set to None for your CollectionView.

4
votes

Please do the following steps

  1. Select your collectionView in interface builder.
  2. Go to the size inspector
  3. Change estimated size to none as shown in the image.

Hopefully it will fix the problem

2
votes

You can use collectionView delegate

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    return CGSize(width:, height:)
}

for this you need to extend UICollectionViewDelegateFlowLayout

2
votes

You have to use UICollectionViewDelegateFlowLayout and implement sizeForItemAt like following.

// MARK: - UICollectionViewDelegateFlowLayout -
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        let sectionInset = (collectionViewLayout as! UICollectionViewFlowLayout).sectionInset

        // Here you can set dynamic height for the individual cell according to your content size.
        let referenceHeight: CGFloat = 100 // Approximate height of your cell

        let referenceWidth = collectionView.safeAreaLayoutGuide.layoutFrame.width
            - sectionInset.left
            - sectionInset.right
            - collectionView.contentInset.left
            - collectionView.contentInset.right

        return CGSize(width: referenceWidth, height: referenceHeight)
    }

Here is an articale about it, you can follow this.

0
votes

Add this property in your collection view or uncheck PrefetchingEnabled property in your nib.

collectionView.isPrefetchingEnabled = false

0
votes

Implement UICollectionViewDelegateFlowLayout method for collection cell size.

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize

Then go to the storyboard and in size inspector make collectionView Estimate size to None