-1

How to design UICollectionView in such a way that it fills the collection view cells vertically (column-wise) instead of filling horizontally (row-wise)?

The collection view should be vertically scrollable, I should not change the size of the collection cell and determine the number of rows dynamically?

shim
  • 9,289
  • 12
  • 69
  • 108
vignan kumar
  • 1
  • 1
  • 1
  • 3

2 Answers2

0

I use this code for it:

let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
layout.scrollDirection = .horizontal
myCollectionView.collectionViewLayout = layout   

For limited number of rows you can set collectionView height and set height of cell and spacings. For example, if you need 2 cells in column, you will write:

        layout.sectionInset = UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)
        layout.itemSize = CGSize(width: self.collectionView.frame.size.width / 5, height: self.collectionView.frame.size.height / 2.5)
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0   

you can change this parameters like you need

Sergey Hleb
  • 152
  • 1
  • 16
  • This really helps me, but the collection view should be vertically scrollable and the number of rows is to be determined dynamically in such a way that I should not scroll horizontally. – vignan kumar Mar 23 '18 at 16:44
  • oh, wow. CollectionView is default object with it propertys. I don't no, but you can calculate how many cells show in one row, and according this calculate how many rows you need. – Sergey Hleb Mar 23 '18 at 16:49
  • and do it programmatically, place CollectionView inside scrollView, for example, and set CollectionView.isScrollEnabled = false – Sergey Hleb Mar 23 '18 at 16:51
  • I can do that but the since the scroll direction is horizontal, the cells are horizontally scrollable. – vignan kumar Mar 23 '18 at 16:52
-1
(YOUR_COLLECTIONVIEW_NAME.collectionViewLayout as? UICollectionViewFlowLayout).scrollDirection = .horizontal
Britto Thomas
  • 2,092
  • 1
  • 15
  • 28