ios UIPageControl 点颜色设置的总结
ios pageControl控件没有点颜色的设置,一开始自己还不信,就一个颜色属性修改,苹果不会这么坑吧,做了很多东西你就会慢慢发现苹果就是这样抗,很多功能得自己去完善。
本来在网上找了些资料都是将pageControl的点当做成一个UIImage,本人用的是xcode5.2调试发现,pageControl子控件没有UIImageView,点是一个UIviwe,通过修改view的layer控制形状。知道这点后自己就有了思路了。
思路:
1、写一个继承UIPageControl类
2、重写UIPageControl的 setCurrentPage方法
3、修改当前点的颜色
code:
#import <UIKit/UIKit.h>
@interface CommonPageControl : UIPageControl
{
UIImage *activeImage;
UIImage *inactiveImage;
}
@end
@interface CommonPageControl : UIPageControl
{
UIImage *activeImage;
UIImage *inactiveImage;
}
@end
#import "CommonPageControl.h"
@implementation CommonPageControl
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
-(void)updateDots{
for(int i=0;i<[self.subviews count];i++){
if([(UIView *)[self.subviews objectAtIndex:i] isKindOfClass:[UIView class]]){//目前pageControl控件小点是一个view
UIView *dot=[self.subviews objectAtIndex:i];
if(i==self.currentPage){
dot.backgroundColor=[UIColor whiteColor];
}
else{
dot.backgroundColor = [UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:0.1];
}
}
}
}
//重写基类方法
-(void)setCurrentPage:(NSInteger)currentPage{
[super setCurrentPage:currentPage];
[self updateDots];
}
@end
@implementation CommonPageControl
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
-(void)updateDots{
for(int i=0;i<[self.subviews count];i++){
if([(UIView *)[self.subviews objectAtIndex:i] isKindOfClass:[UIView class]]){//目前pageControl控件小点是一个view
UIView *dot=[self.subviews objectAtIndex:i];
if(i==self.currentPage){
dot.backgroundColor=[UIColor whiteColor];
}
else{
dot.backgroundColor = [UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:0.1];
}
}
}
}
//重写基类方法
-(void)setCurrentPage:(NSInteger)currentPage{
[super setCurrentPage:currentPage];
[self updateDots];
}
@end
调用:
CGRect rect;
rect.origin.x = myScrollView.frame.origin.x;
rect.origin.y = self.frame.size.height-20;
rect.size.width = myScrollView.frame.size.width;
rect.size.height = 20;
rect.origin.x = myScrollView.frame.origin.x;
rect.origin.y = self.frame.size.height-20;
rect.size.width = myScrollView.frame.size.width;
rect.size.height = 20;
myPagecontrol = [[CommonPageControl alloc] initWithFrame:rect];
myPagecontrol.userInteractionEnabled=NO;
myPagecontrol.userInteractionEnabled=NO;