iOS14.0 adaptation, record!
adaptationUIPageControl
- Problem:
iOS14
After deletingpageImage
andcurrentPageImage
, you can no longerKVC
modify the picture of the controller. - Solution:
With the help of theiOS14
new fieldpreferredIndicatorImage
, we can modify the size and shape of the indicator dot, and usecurrentPageIndicatorTintColor
andpageIndicatorTintColor
to confirm the color of the dot in different states.
The color of the final display is determined byTintColor
andpreferredIndicatorImage
jointly, so here will bepreferredIndicatorImage
set to white. - Some code examples::
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;
}