Wednesday, November 11, 2026
HomeiOS DevelopmentHow can I make dynamic cell width for default format with horizontal...

How can I make dynamic cell width for default format with horizontal scrolling and equal cell house with main Swift + iOS?

[ad_1]

I’m wanting a Horizontal Scrolling UICollectionView with 3 rows and dynamic width, cell not align to the left just like the picture under as android horizontal scrolling :

enter image description here

SetUpCollectionView Code-

func setUpCollectionView() {
    let format = UICollectionViewFlowLayout()
    format.scrollDirection = UICollectionView.ScrollDirection.horizontal
    format.minimumInteritemSpacing = 16
    format.minimumLineSpacing = 12

    format.estimatedItemSize = UICollectionViewFlowLayout.automaticSize
    collectionView.semanticContentAttribute = UISemanticContentAttribute.forceLeftToRight
    collectionView.contentInsetAdjustmentBehavior = .all the time
    collectionView.collectionViewLayout = format}

Right here is collectionView code:

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

    let extraSpace : CGFloat = 50.0

    return CGSize(width: (self.viewModels?.car_problems?[indexPath.item].drawback?.dimension(withAttributes: [NSAttributedString.Key.font : UIFont.systemFont(ofSize: 17)]).width ?? 34) + extraSpace, peak: 32)

}

func collectionView(_ collectionView: UICollectionView, format collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt part: Int) -> CGFloat {
    return 16
}

func collectionView(_ collectionView: UICollectionView, format collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt part: Int) -> CGFloat {
    return 12
}

func collectionView(_ collectionView: UICollectionView, format collectionViewLayout: UICollectionViewLayout, insetForSectionAt part: Int) -> UIEdgeInsets {
    return UIEdgeInsets(prime: 5, left: 16, backside: 0, proper: 16)
}

Anticipated consequence:

enter image description here

[ad_2]

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments