I have a UIPageControl in my application that looks perfectly fine with around 10 pages (dots), it\'s possible however for the user to add many different views, and so the numbe
My solution is setting the maximum number of dots and showing the dot before the last one until the user reaches the end. If the current page number is bigger than maximum dot count we show the one before the last one until the user reaches the end.
var maxDotCount = 8 //Global constant.
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
let pageWidth = collectionView.frame.size.width
let currentPage = collectionView.contentOffset.x / pageWidth
let pageControlsNumberOfPages = pageControl.numberOfPages - 1
let dataSourceCount = CGFloat(dataSourceArr.count - 1)
let maxDotCountReduced = CGFloat(maxDotCount - 1)
if (dataSourceCount > maxDotCountReduced) {
if (currentPage >= maxDotCountReduced) {
if (currentPage == dataSourceCount) {
pageControl.currentPage = pageControlsNumberOfPages
} else {
pageControl.currentPage = pageControlsNumberOfPages - 1
}
return
}
}
pageControl.currentPage = Int(currentPage)
}