0

I have created custom class with subclasss of 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 file

@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];
}

the following line never executed.I am using iOS 8.3

for (int i = 0; i < [self.subviews count]; i++)

here never get into that loop..

i dont know whats going on here..

i am following this Link

i have called at my uipageviewcontroller like this...

pageControl=[[customclass alloc]init];
    [pageControl setCurrentPage:0];
Community
  • 1
  • 1
karthikeyan
  • 3,821
  • 3
  • 22
  • 45

1 Answers1

0

First, I don't think you need an UIPageControl in your custom UIPageControl.

Then you need to set the number of page of your custom page control, so in the viewDidLoad of your UIViewController, you will have something like that (I recommend you to change the name of your custom UIPageControl with MyPageControl for example):

customclass *pc = [[customclass alloc] init];
pc.center = self.view.center; // Set the position here
[pc setNumberOfPages:10];
[pc setCurrentPage:0];
[self.view addSubview:pc];

Finally, what about this other solution:

UIPageControl *pc = [[UIPageControl alloc] init]; 
pc.center = CGPointMake(200, 200); // Set the position here
pc.currentPageIndicatorTintColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"dots"]];
pc.pageIndicatorTintColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"off"]];
[pc setNumberOfPages:10];
[pc setCurrentPage:0];
Y.Bonafons
  • 2,329
  • 13
  • 20