简体   繁体   中英

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)
    }```

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? ie 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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM