IOS14 UIPageControl设置_pageimage奔溃

众所周知,苹果对通过kvc设置私有变量这个事情越来越敏感,从IOS13的对textField的placeholder设置字体大小颜色等就已经看出,今年又对UIPageControl下手了,通过kvc设置私有变量_pageImage和_currentPageImage会引起程序奔溃,具体见以下代码:

[pageControl setValue:[UIImage imageNamed:@"page_unselect"] forKeyPath:@"_pageImage"];
[pageControl setValue:[UIImage imageNamed:@"page_select"] forKeyPath:@"_currentPageImage"];

解决方案:

通过自定义pageControl达成预期目标,可以参考著名视图轮播框架SDCycleScrollView里的自定义TAPageControl,下面放出github地址,可直接拿过来用

https://github.com/gsdios/SDCycleScrollView

用法和UIPageControl几乎一模一样,代码如下:

- (TAPageControl *)pageControl {
    if (!_pageControl) {
        _pageControl = ({
            TAPageControl *pageControl = [[TAPageControl alloc] init];
            pageControl.currentDotImage = [UIImage imageNamed:@"page_select"];
            pageControl.dotImage = [UIImage imageNamed:@"page_unselect"];
            pageControl.numberOfPages = 3;
            pageControl.currentPage = 0;
            pageControl;
        });
    }
    return _pageControl;
}

你可能感兴趣的:(IOS14 UIPageControl设置_pageimage奔溃)