我创建了自定义类,其子类为 UIPageControl
customClass.h
@interface customclass : UIPageControl
{
UIImage* activeImage;
UIImage* inactiveImage;
}
@property(nonatomic, retain) UIImage* activeImage;
@property(nonatomic, retain) UIImage* inactiveImage;
@property (weak, nonatomic) IBOutlet UIPageControl *pageController;
@end
customclass.m文件
@implementation customclass
@synthesize activeImage,inactiveImage;
-(id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if(self)
{
activeImage = [UIImage imageNamed:@"dots.png"];
inactiveImage = [UIImage imageNamed:@"off.png"];
}
return self;
}
-(id)init
{
self = [super init];
if(self)
{
activeImage = [UIImage imageNamed:@"dots.png"];
inactiveImage = [UIImage imageNamed:@"off.png"];
}
return self;
}
-(void)updateDots
{
for (int i = 0; i < [self.subviews count]; i++)
{
UIImageView * dot = [self imageViewForSubview: [self.subviews objectAtIndex: i]];
if (i == self.currentPage) dot.image = activeImage;
else dot.image = inactiveImage;
}
}
- (UIImageView *) imageViewForSubview: (UIView *) view
{
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, 17,17)];
[view addSubview:dot];
}
}
else
{
dot = (UIImageView *) view;
}
return dot;
}
-(void)setCurrentPage:(NSInteger)page
{
[super setCurrentPage:page];
[self updateDots];
}
以下行从未执行过。我正在使用iOS 8.3
for (int i = 0; i < [self.subviews count]; i++)
这里永远不会进入loop
..
我不知道这里发生了什么..
我正在关注此Link
我已经在我的uipageviewcontroller上打电话了......
pageControl=[[customclass alloc]init];
[pageControl setCurrentPage:0];
答案 0 :(得分:0)
首先,我认为您的自定义UIPageControl
中不需要UIPageControl
。
然后您需要设置自定义页面控件的页面数量,因此在viewDidLoad
的{{1}}中,您将拥有类似的内容(我建议您更改您的名称)自定义UIViewController
,例如UIPageControl
:
MyPageControl
最后,这个其他解决方案呢:
customclass *pc = [[customclass alloc] init];
pc.center = self.view.center; // Set the position here
[pc setNumberOfPages:10];
[pc setCurrentPage:0];
[self.view addSubview:pc];