Try to search your question here, if you can't find : Ask Any Question Now ?

Swift using estimatedItemSize for cell breaks cell width during scrolling

HomeCategory: stackoverflowSwift using estimatedItemSize for cell breaks cell width during scrolling
Avatarrupesh asked 3 months ago

ios version 11/12

Trying to make dynamic cell height for UIViewController (cell contains image with fixed height and label that can be multiline because of that label view-cell should be able dynamically change height). I’m using Storyboard to construct basic UI with some constrains that makes cell min width 300.

Getting strange UI behaviour during vertical scrolling in UIViewController. Some cells(that are not in the UI initially) resized to the size defined in Storyboard constrains and ignoring estimatedItemSize

import UIKit

class StartController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {

    let offers = SearchCategoryOffer.mockOffers
    @IBOutlet weak var collectionView: UICollectionView!


    override func viewDidLoad() {
        super.viewDidLoad()
        navigationController?.view.backgroundColor = UIColor.white

        let titleImageView = NavigationImageView()
        titleImageView.image = UIImage(named: "logo")
        navigationItem.titleView = titleImageView

        let w = self.collectionView.frame.width - 30

        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.translatesAutoresizingMaskIntoConstraints = false


        if let layout = self.collectionView.collectionViewLayout as? UICollectionViewFlowLayout {
            layout.sectionInsetReference = .fromLayoutMargins
            layout.sectionInset = UIEdgeInsetsMake(20, 5, 20, 5)
            layout.minimumLineSpacing = 26
            layout.minimumInteritemSpacing = 0
            layout.estimatedItemSize = CGSize(width: w, height: 300) // cell size
        }
    }

    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }

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

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

        let cell: OfferCell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! OfferCell
        cell.listTitle?.text = offers[indexPath.row].name
        return cell
    }

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let _: OfferCell = collectionView.cellForItem(at: indexPath) as! OfferCell
    }

    func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
        let _: OfferCell = collectionView.cellForItem(at: indexPath) as! OfferCell
    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // bla-bla-bla
    }
}

class NavigationImageView: UIImageView {
    override func sizeThatFits(_ size: CGSize) -> CGSize {
        return CGSize(width: 133, height: 35)
    }
}

enter image description here

1 Answers
Best Answer
AvatarFernando answered 3 months ago
Your Answer

13 + 20 =

Popular Tags

WP Facebook Auto Publish Powered By : XYZScripts.com