Friday, April 17, 2026
HomeiOS Developmentios - UICollectionView crashes when merchandise measurement is automated and header is...

ios – UICollectionView crashes when merchandise measurement is automated and header is proven

[ad_1]

I encounter a bizarre error that after I need to set the estimatedItemSize property of the UICollectionView to UICollectionViewFlowLayout.automaticSize and present a piece header on the identical time.

With the under code, the app can present the UICollectionView correctly. Nevertheless, as soon as the person scrolls on the UICollectionView, the app crashes with a recursive name to the perform updateVisibleCellsNow.

I discover a workaround answer from one other StackOverflow query by setting the estimatedItemSize from automaticSize to none. Nevertheless, I need to preserve the auto-layout options set on the UICollectionViewCell as a substitute of calculating the cell top myself. Is there any higher answer? Thanks.

enter image description here

Right here is my code concerning the ViewController

import UIKit
class DemoCollectionViewController: UIViewController {
    
    lazy personal var collectionView: UICollectionView = { [weak self] in
        guard let strongSelf = self else { return UICollectionView() }
        
        // Setup of `UICollectionViewFlowLayout`
        let flowLayout = UICollectionViewFlowLayout()
        

        // ********* That is the road that causes crash *********
        flowLayout.estimatedItemSize = UICollectionViewFlowLayout.automaticSize
        // ******************************************************

        flowLayout.headerReferenceSize = CGSize(width: getScreenWidth(), top: 44.0)
        flowLayout.sectionHeadersPinToVisibleBounds = true
        
        // Setup of `UICollectionView`
        let collectionView = UICollectionView(body: .zero, collectionViewLayout: flowLayout).disableFrameToAutoLayout()
        collectionView.dataSource = strongSelf
        collectionView.register(ProfileThumbnailCollectionViewCell.self, forCellWithReuseIdentifier: "cellId")
        collectionView.register(UICollectionReusableView.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "headerId")
        
        return collectionView
    }()
    
    
    override func viewDidLoad() {
        tremendous.viewDidLoad()
        
        // Setup of the UICollectionView
        view.addSubview(collectionView)
        NSLayoutConstraint.activate([
            collectionView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
            collectionView.rightAnchor.constraint(equalTo: view.safeAreaLayoutGuide.rightAnchor),
            collectionView.bottomAnchor.constraint(equalTo: view.bottomAnchor),
            collectionView.leftAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leftAnchor),
        ])
    }
}

extension DemoCollectionViewController: UICollectionViewDataSource {
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 10
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection part: Int) -> Int {
        return 20
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellId", for: indexPath) as! ProfileThumbnailCollectionViewCell
        cell.contentView.backgroundColor = .yellow
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind variety: String, at indexPath: IndexPath) -> UICollectionReusableView {

        swap variety {
        case UICollectionView.elementKindSectionHeader:
            let supplementaryView = collectionView.dequeueReusableSupplementaryView(ofKind: variety, withReuseIdentifier: "headerId", for: indexPath)
            supplementaryView.backgroundColor = .pink
            return supplementaryView
        default:
            return UICollectionReusableView()
        }
    }
}

Right here is my code concerning the ProfileThumbnailCollectionViewCell:

class ProfileThumbnailCollectionViewCell: UICollectionViewCell {
    
    personal let profileThumbnailImageView: UIImageView = {
        let profileThumbnailImageView = UIImageView()
        profileThumbnailImageView.translatesAutoresizingMaskIntoConstraints = false
        profileThumbnailImageView.backgroundColor = .pink
        profileThumbnailImageView.layer.cornerRadius = 60
        profileThumbnailImageView.layer.masksToBounds = true
        return profileThumbnailImageView
    }()
    
    personal let editPenButton: UIButton = {
        let editPenButton = UIButton()
        editPenButton.translatesAutoresizingMaskIntoConstraints = false
        editPenButton.backgroundColor = .mainGreen
        editPenButton.layer.cornerRadius = 16
        editPenButton.layer.masksToBounds = true
        return editPenButton
    }()
    
    override init(body: CGRect) {
        tremendous.init(body: body)
        
        // Content material View
        contentView.backgroundColor = .yellow
        NSLayoutConstraint.activate([
            contentView.widthAnchor.constraint(equalToConstant: UIScreen.main.bounds.width)
        ])
        
        // Profile Thumbnail ImageView
        contentView.addSubview(profileThumbnailImageView)
        NSLayoutConstraint.activate([
            profileThumbnailImageView.centerXAnchor.constraint(equalTo: contentView.centerXAnchor),
            profileThumbnailImageView.widthAnchor.constraint(equalToConstant: 120),
            profileThumbnailImageView.heightAnchor.constraint(equalToConstant: 120),
            profileThumbnailImageView.topAnchor.constraint(equalTo: contentView.topAnchor, constant: 25),
            profileThumbnailImageView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: -25)
        ])
        
        // Edit thumbnail pen ImageView
        contentView.addSubview(editPenButton)
        NSLayoutConstraint.activate([
            editPenButton.widthAnchor.constraint(equalToConstant: 32),
            editPenButton.heightAnchor.constraint(equalToConstant: 32),
            editPenButton.rightAnchor.constraint(equalTo: contentView.rightAnchor),
            editPenButton.rightAnchor.constraint(equalTo: contentView.rightAnchor)
        ])
    }
    
    required init?(coder: NSCoder) {
        fatalError("This class must be inited from code as a substitute of a nib file; init(coder:) has not been carried out")
    }
}

[ad_2]

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments