简体   繁体   中英

UICollectionView cell fixed width and dynamic height using constraints in swift

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.

在此处输入图片说明 在此处输入图片说明

您需要确保您的 CollectionView 的Estimate Size设置为None

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

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

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 .

Hope it helps you. Happy Coding

在您的集合视图中添加此属性或取消选中笔尖中的 PrefetchingEnabled 属性。

collectionView.isPrefetchingEnabled = false

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM