UIPageControl是苹果自带的一个分页控制器,但是它的样式可能不会满足我们所有的需求,系统自带的样式是圆形且大小固定的。但是我们可以通过重写UIPageControl来达到我们想要的效果.
具体步骤
- 创建一个类,继承于UIPageControl
- 重写init方法,记得设置 self.userInteractionEnabled = NO;
否则UIPageController可以被点击)

- 最后重写set方法,修改他的样式(大小,圆角都可以设置,位置,背景图等)

我最终想要的效果是让它变成方形的,你们需要什么样式根据需求自定义就行啦

核心代码
- (instancetype)init
{
self = [super init];
if (self) {
self.userInteractionEnabled = NO;
}
return self;
}
-(void)setCurrentPage:(NSInteger)currentPage{
[super setCurrentPage:currentPage];
for (NSUInteger subviewIndex = 0; subviewIndex < [self.subviews count]; subviewIndex++) {
UIImageView* subview = [self.subviews objectAtIndex:subviewIndex];
// CGSize size;
// size.height = 12;
//
// size.width = 12;
//
// [subview setFrame:CGRectMake(subview.frame.origin.x, subview.frame.origin.y,
//
// size.width,size.height)];
if (subviewIndex == currentPage)
// subview.image =[UIImage imageNamed:@"60.png"];
{ subview.layer.cornerRadius = 0;
subview.layer.masksToBounds = YES;
}
else
{ //subview.image =[UIImage imageNamed:@"60.png"];
subview.layer.cornerRadius = 0;
subview.layer.masksToBounds = YES;
}
}
~~~
网友评论