What's the best way of handling a UIPageControl that has so many dots they don't all fit on the screen

后端 未结 5 1265
夕颜
夕颜 2021-02-08 22:51

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

5条回答
  •  孤街浪徒
    2021-02-08 23:30

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

提交回复
热议问题