版權聲明:本文為博主原創(chuàng)文章推盛,遵循 CC 4.0 BY-SA 版權協(xié)議,轉(zhuǎn)載請附上原文出處鏈接和本聲明古毛。
本文鏈接:https://blog.csdn.net/a416863220/article/details/43407517
近來联贩,項目中需要定制UIPageControl樣式抵赢,目前知道的方式大概如下葫辐,這里簡單概括:
一刨裆、修改顏色聂示,最簡單的方式:
// 設置非選中頁的圓點顏色
_pageControl.pageIndicatorTintColor = [UIColor redColor];
// 設置選中頁的圓點顏色
_pageControl.currentPageIndicatorTintColor = [UIColor blueColor];
二告唆、修改靜態(tài)圖標樣式棺弊,可以采用KVO方式,這種弊端是只能靜態(tài)顯示擒悬,無法動態(tài)替換模她,且私有,危險:
[_pageControl setValue:[UIImage imageNamed:@"image1"] forKeyPath:@"_pageImage"];
[_pageControl setValue:[UIImage imageNamed:@"image2"] forKeyPath:@"_currentPageImage"];
三懂牧、替換系統(tǒng)默認圖標侈净,自定義樣式:
常規(guī)方式便是創(chuàng)建一個類,繼承于UIPageControl僧凤,重新set方式畜侦,這里寫下核心代碼,也是目前網(wǎng)絡上的方式:
-(void) updateDots {
for (int i = 0; i < [self.subviews count]; i++)
{
UIImageView *dot = [self.subviews objectAtIndex:i];
if (i == self.currentPage) {
dot.image = activeImage;
}
else
dot.image = inactiveImage;
}
結果是程序崩潰躯保,歸咎原因:
這種方式及其危險的夏伊,因為[self.subviews objectAtIndex:i] class 為UIView, 而 UIImageView *dot = [self.subviews objectAtIndex:i]; 這句是將一個UIView轉(zhuǎn)換為UIImageView, 故容易crash 掉,特此放棄如上國內(nèi)方式吻氧;
四溺忧、完善上面第三種方式,轉(zhuǎn)為安全的替換方式盯孙,核心代碼如下:
for (UIView *subview in view.subviews) {
if ([subview isKindOfClass:[UIImageView class]]) {
dot = (UIImageView *)subview;
break;
}
}
采用如上安全方式鲁森,可有效防止崩潰,至此完成項目需求振惰,效果如下:
五歌溉、這里把整個代碼放出來,幫助大家少走彎路骑晶,具體代碼如下:
1痛垛、.h文件
#import <UIKit/UIKit.h>
@interface CustomPageControl : UIPageControl
@property (nonatomic, strong) UIImage *currentImage;
@property (nonatomic, strong) UIImage *inactiveImage;
@property (nonatomic, assign) CGSize currentImageSize;
@property (nonatomic, assign) CGSize inactiveImageSize;
@end
2、.m文件
#import "CustomPageControl.h"
@implementation CustomPageControl
- (instancetype)init{
self = [super init];
if (self) {
self.userInteractionEnabled = NO;
}
return self;
}
- (void)setCurrentPage:(NSInteger)currentPage{
[super setCurrentPage:currentPage];
[self updateDots];
}
- (void)updateDots{
for (int i = 0; i < [self.subviews count]; i++) {
UIImageView *dot = [self imageViewForSubview:[self.subviews objectAtIndex:i] currPage:i];
if (i == self.currentPage){
dot.image = self.currentImage;
dot.size = self.currentImageSize;
}else{
dot.image = self.inactiveImage;
dot.size = self.inactiveImageSize;
}
}
}
- (UIImageView *)imageViewForSubview:(UIView *)view currPage:(int)currPage{ UIImageView *dot = nil;
if ([view isKindOfClass:[UIView class]]) {
for (UIView *subview in view.subviews) {
if ([subview isKindOfClass:[UIImageView class]]) {
dot = (UIImageView *)subview;
break;
}
}
if (dot == nil) {
dot = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, view.frame.size.width, view.frame.size.height)];
[view addSubview:dot];
}
}else {
dot = (UIImageView *)view;
}
return dot;
}
@end
3桶蛔、使用方式:
CustomPageControl *pageCtrl = [[CustomPageControl alloc] initWithFrame:CGRectMake(0, 10, mScreenWidth, 5)];
[pageCtrl.numberOfPages = cardSwitchViewAry.count;
pageCtrl.currentPage = 0;
pageCtrl.userInteractionEnabled = NO;
pageCtrl.inactiveImage = [UIImage imageNamed:@"ex_page_1"];
pageCtrl.inactiveImageSize = CGSizeMake(5, 5);
pageCtrl.currentImage = [UIImage imageNamed:@"ex_page_2"];
pageCtrl.currentImageSize = CGSizeMake(10, 5);
//去掉系統(tǒng)自帶樣式
pageCtrl.currentPageIndicatorTintColor = [UIColor clearColor];
pageCtrl.pageIndicatorTintColor = [UIColor clearColor];
至此匙头,定制UIPageControl的正確姿勢已了解,希望對大家有用仔雷。