iosobjective-cswiftios14uipagecontrol

iOS 14 UIPageControl unselected page are treated as currentPage


When I use setIndicatorImage to set image for the UIPageControl, unselected page's tint color are set to the currentPageIndicatorTintColor, but current page's tint color is default indicator tint color.

enter image description here

    self.customPageControl.currentPage = currentPage;
    
    if (@available(iOS 14.0, *)) {
        _pageControl.pageIndicatorTintColor = [UIColor blueColor];
        _pageControl.currentPageIndicatorTintColor = [UIColor greenColor];

        for(int i=0; i<self.pageControl.numberOfPages; i++){
            if(i == currentPage){
                [self.pageControl setIndicatorImage:[UIImage imageNamed:@"pageCtrlSelected"] forPage:i];
            }
            else{
                [self.pageControl setIndicatorImage:[UIImage imageNamed:@"pageCtrlUnselect"] forPage:i];
            }
        }
        
    } else {
        // Fallback on earlier versions
    }

Solution

  • Finally I solved this by call set current page after set indicator image loop. The code snipper is like below.

    if (@available(iOS 14.0, *)) {
        _pageControl.pageIndicatorTintColor = [UIColor blueColor];
        _pageControl.currentPageIndicatorTintColor = [UIColor greenColor];
    
        for(int i=0; i<self.pageControl.numberOfPages; i++){
            if(i == currentPage){
               [self.pageControl setIndicatorImage:[UIImage imageNamed:@"pageCtrlSelected"] forPage:i];
            }
            else{
               [self.pageControl setIndicatorImage:[UIImage imageNamed:@"pageCtrlUnselect"] forPage:i];
            }
        }
            
    } else {
        // Fallback on earlier versions
    }
    
    /// It's important that set currentPage after set indicator image !!!!
    self.customPageControl.currentPage = currentPage;