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

后端 未结 5 1278
夕颜
夕颜 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:35

    I had to implement UIPageControl under my horizontal collection view which had 30 items. So what i did to reduce the number of dots by fixing the number of dots and scroll in those dots only. Follow the code:

    @IBOutlet var pageControl: UIPageControl!
    var collectionDataList = [String]
    let dotsCount = 5
    
    override func viewDidAppear(_ animated: Bool) {
        if collectionDataList.count < dotsCount {
            self.pageControl.numberOfPages = collectionDataList.count
        } else {
            self.pageControl.numberOfPages = dotsCount
        } 
    }
    
    func scrollViewDidScroll(_ scrollView: UIScrollView)
    {
       let pageWidth = scrollView.frame.width
       self.currentPage = Int((scrollView.contentOffset.x + pageWidth / 2) / pageWidth)
       self.pageControl.currentPage = self.currentPage % dotsCount
    
    }
    

提交回复
热议问题