0

I am facing issue with UICollectionView. I want all cell should have fixed size. But I cannot achieve requirement.

I want output like : Actual Requirement

And what I am getting is like : Wrong Design

Here is my code :

private var totalColumns: Int = 3
private var cellSpace: CGFloat = 10.0
private var width: CGFloat {
    var temp = (CGFloat(self.totalColumns - 1) * self.cellSpace)
    temp = (self.collectionPreference?.frame.size.width ?? SCREEN_WIDTH) - temp
    temp = temp / CGFloat(totalColumns)
    return temp
}

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

    return CGSize(width: width, height: width)
}

//    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
//        return UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)
//    }

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
    return cellSpace
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
    return cellSpace
}

Before iOS 13, I can achieve same UI with same code (and at that time I was searching that how can we design cell according to its content). And now in iOS 13, I am struggling to achieve fixed size UI.... Very funny...

What I have taken : Subviews in cell

UIImageView constraints : UIImageView Constraints

ULabel constraints : UILabel Constraints

Can anyone have solution ?

VRAwesome
  • 4,721
  • 5
  • 27
  • 52

1 Answers1

0

Try this:

override func viewDidLoad() {
    super.viewDidLoad()

    let cellSize = CGSize(width:80 , height:80) // your cell size

    let layout = UICollectionViewFlowLayout()
    layout.scrollDirection = .vertical //.horizontal
    layout.itemSize = cellSize
    layout.sectionInset = UIEdgeInsets(top: 1, left: 1, bottom: 1, right: 1)
    layout.minimumLineSpacing = 1.0
    layout.minimumInteritemSpacing = 1.0
    collectionPreference.setCollectionViewLayout(layout, animated: true)

    collectionPreference.reloadData()
}
  • comment if you have any issue on this.
Nick
  • 875
  • 6
  • 20