Search code examples
iosswiftuicollectionviewuipagecontrol

Page control switches page before completely going to the next page


I have created a page control for a collection view, each collection view cell is the height and width of the screen. Here is the setup of the page control:

// Setup the page control
func setupPageControl() {

    pageControl.pageIndicatorTintColor = cellColor
    pageControl.currentPageIndicatorTintColor = themeColor

    view.addSubview(pageControl)
    addPageControlConstraints()
}

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    pageControl.numberOfPages = collectionViewCellData.count
    return collectionViewCellData.count
}

func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
    pageControl.currentPage = indexPath.row
}

There is a small problem with the page control, check this out:

Screen recording

Notice how the page control changes when I haven't even completely gone to the next page, how can I solve this problem?


Solution

  • You need to use UIPageViewControllerDelegate's method "didFinishAnimating: previousViewControllers: transitionCompleted:" and use the transitionCompleted boolean that helps you to update your page

    OR

    Refer this link to use UIPageControl