First add UIPageControl
to your storyboard with UICollectionView
, and then connect them as outputs to the view controller.
@IBOutlet var pageControl: UIPageControl!
@IBOutlet var collectionView: UICollectionView!
Adjust your method numberOfItemsInSection
in UICollectionViewDataSource
to set the page control counter to always equal the number of cells in the collection view.
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
let count = ...
pageControl.numberOfPages = count
pageControl.isHidden = !(count > 1)
return count
}
, UIScrollViewDelegate
, , UICollectionView
. UICollectionViewController
, .
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
pageControl?.currentPage = Int(scrollView.contentOffset.x) / Int(scrollView.frame.width)
}
func scrollViewDidEndScrollingAnimation(_ scrollView: UIScrollView) {
pageControl?.currentPage = Int(scrollView.contentOffset.x) / Int(scrollView.frame.width)
}
, a UICollectionView
a UIScrollView
.