Sunday, August 14, 2022
HomeiOS DevelopmentHow can I make dynamic cell width for default structure with horizontal...

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


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 structure = UICollectionViewFlowLayout()
    structure.scrollDirection = UICollectionView.ScrollDirection.horizontal
    structure.minimumInteritemSpacing = 16
    structure.minimumLineSpacing = 12

    structure.estimatedItemSize = UICollectionViewFlowLayout.automaticSize
    collectionView.semanticContentAttribute = UISemanticContentAttribute.forceLeftToRight
    collectionView.contentInsetAdjustmentBehavior = .at all times
    collectionView.collectionViewLayout = structure}

Right here is collectionView code:

func collectionView(_ collectionView: UICollectionView, structure 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, structure collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt part: Int) -> CGFloat {
    return 16
}

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

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

Anticipated outcome:

enter image description here

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments