iOS14.0适配,记录!
适配UIPageControl
- 出现的问题:
iOS14
以后删除了pageImage
与currentPageImage
,不能再通过KVC
修改控制器的图片。 - 解决方案:
借助iOS14
新增字段preferredIndicatorImage
,我们可以修改指示器小圆点的大小以及形状,借助currentPageIndicatorTintColor
与pageIndicatorTintColor
来确认不同状态下小圆点的颜色。
最终展示的颜色是由TintColor
以及preferredIndicatorImage
共同决定的,因此这里将preferredIndicatorImage
设置为白色。 - 部分代码示例:::
UIPageControl *pageControl = [[UIPageControl alloc]init];
pageControl.backgroundColor = [UIColor clearColor];
pageControl.numberOfPages = 1;
if (@available(iOS 14.0,*)) {
pageControl.preferredIndicatorImage = [self imageWithColor:[UIColor whiteColor]];
pageControl.currentPageIndicatorTintColor = [UIColor redColor];
pageControl.pageIndicatorTintColor = [UIColor blueColor];
}else{
[pageControl setValue:[UIImage imageNamed:@"pageControllDot"]forKeyPath:@"pageImage"];
[pageControl setValue:[UIImage imageNamed:@"pageControllCurrent"]forKeyPath:@"currentPageImage"];
}
// 绘制小圆点的形状
- (UIImage *)imageWithColor:(UIColor *)color {
CGRect rect = CGRectMake(0.0f, 0.0f, 12.f, 2.f);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [color CGColor]);
CGContextFillRect(context, rect);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}