Powered By Blogger

Wednesday, March 7, 2012

Custom UIPageControl

Hello,
This code is useful for creating your custom page control having different color.


#import <Foundation/Foundation.h>


@interface GrayPageControl : UIPageControl
{
    UIImage* activeImage;
    UIImage* inactiveImage;
}
@property(nonatomic, retain) UIImage* activeImage;
@property(nonatomic, retain) UIImage* inactiveImage;
@end


#import "GrayPageControl.h"


@implementation GrayPageControl
@synthesize activeImage;
@synthesize inactiveImage;

-(id) initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    activeImage = [UIImage imageNamed:@"active_page_image.png"];
    inactiveImage = [UIImage imageNamed:@"inactive_page_image.png"];
    return self;
}

-(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;
    }
}

-(void)setCurrentPage:(NSInteger)page
{
    [super setCurrentPage:page];
    [self updateDots];
}

@end

After creating these two files in your project you have to declare:
 IBOutlet UIPageControl *nameObject;
in which you want to use this control and then you have to set his class in .XIB file as "GrayPageControl".



If any one has query then place you it over here.

Thanks and Regards,
Nilesh Prajapati
 

3 comments:

  1. -[UIView setImage:]: unrecognized selector sent to instance 0x17816b400' crashes on iOS 7, because these dots aren't UIImageView anymore!

    ReplyDelete
    Replies
    1. Hi,
      Ok. So do one more thing before applying images.

      UIImageView* dot = [self.subviews objectAtIndex:i];
      if ([dot isKindOfClass:[UIImageView class]]) {
      if (i == self.currentPage)
      dot.image = activeImage;
      else
      dot.image = inactiveImage;
      }

      Place this code and check it again..

      Good Luck..!!

      Delete