faby ferrara
faby ferrara

Reputation: 1

PageControl in collectionView swift

I created a control page for my collectionView of 12 cells. I would like these cells to be distributed in 2 pages, in a grid of 3 columns and 2 rows. but with the current code the 12 cells are arranged on one row. how can I fix it? i think i have to change the layout of my collection, but i can't change anything from the storyboard. this is my code for pageControl if it helps.

 var thisWidth: CGFloat = 0

    override func awakeFromNib() {
        super.awakeFromNib()

        thisWidth = CGFloat(self.view.frame.width)
        cartaCollectionView.delegate = self
        cartaCollectionView.dataSource = self

        pageControl.hidesForSinglePage = true
    }

    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        pageControl.currentPage = Int(scrollView.contentOffset.x) / Int(scrollView.frame.width)
    }```

Upvotes: 0

Views: 178

Answers (1)

Steve Hancocks
Steve Hancocks

Reputation: 209

Start by looking at the UICollectionViewDelegateFlowLayout method

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

Calculating and setting an appropriate size for the cells within your collection will help you to get the required columns / cells.

Does the order of your cells matter? i.e do you want cells 1-6 on the first page? If so you'll need to build a custom flowlayout. Maybe you could use an arrangement of UIStackviews instead?

Upvotes: 0

Related Questions