To change page indicator dot in iPhone pagecontrol follow the below steps and set of codes.
- Open The Xcode and Create a new project Name it as MyPageControl
- select the project and right click and add new files and select a cocoatouch File and inherit with it pageControl name it as MyCustomPageControl.
- add a UIPageCotrol on mainStroryBoard and a UIScrollView.
chage UIPageCotrol class to MyCustomPageControl. Also map them to appropriate Outlet(Pagecontroller with pageControl and UIScrollView With pageControl) And when creating the outlet for pagecontrol in ViewController.h , also import the file MyCustomPageControl like:
ViewController.h:
#importMyCustomPageControl.h
{
__weakIBOutlet myCustomPageControl *pageControl;
__weak IBOutlet UIScrollView *scrollView;
}
NSArray *pageImages;
File MyCustomPageControl is like this:
MyCustomPageControl.h file:
#import <UIKit/UIKit.h>
@interface MyCustomPageControl: UIPageControl
{
UIImage* activeImage;
UIImage* inactiveImage;
}
@end
MyCustomPageContorl .m file is Like This:
#import "myCustomPageControl.h"
@implementation myCustomPageControl
-(id) initWithCoder:(NSCoder *)aDecoder
{
NSLog(@"Initializing the mycustom");
self = [super initWithCoder:aDecoder];
activeImage = [UIImage imageNamed:@"selectedImage"] ;
inactiveImage = [UIImage imageNamed:@"unselectedImage"];
return self;
}
-(void) dotsUpdate
{
NSLog(@"self.subviews=%@",self.subviews);
for (int i = 0; i < [self.subviews count]; i++)
{
UIImageView *dotImageView = [self imageViewForSubview:[self.subviews objectAtIndex:i]];
if (i == self.currentPage)
{
[dotImageView setFrame:CGRectMake(0.0,0.0, 10.0, 10.0)];// size for the selected dot
dotImageView.image = activeImage;
}
else
{
[dotImageView setFrame:CGRectMake(0.0, 0.0, 10, 10)]; //size for the unselected dot
dotImageView.image = inactiveImage;
}
}
}
- (UIImageView *) imageViewForSubview: (UIView *) view
{
UIImageView *dot = nil;
if ([view isKindOfClass: [UIView class]])
{
for (UIView *subview in view.subviews)
{
//[subview setBackgroundColor:[UIColor blackColor]];
if ([subview isKindOfClass:[UIImageView class]])
{
dot = (UIImageView *)subview;
break;
}
}
if (dot == nil)
{
dot = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f, 0.0f,view.frame.size.width, view.frame.size.height)];
[view addSubview:dot];
}
}
else
{
dot = (UIImageView *) view;
}
return dot;
}
-(void) setCurrentPage:(NSInteger)page
{
[super setCurrentPage:page];
[self dotsUpdate];
}
@end
And ViewController.m File change it to:
#import "ViewController.h"
@interface ViewController()<UIScrollViewDelegate,UIScrollViewAccessibilityDelegate>
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
scrollView setPagingEnabled:YES];
pageImages = @[@"image1",@" image2",@" image3"];
scrollView.contentSize=CGSizeMake(self.view.frame.size.width*3.0, 0);
self.scrollView.delegate = self;
for(int i = 0;i < pageImages.count;i++)
{
[scrollView addSubview:[self guideImage:[pageImages objectAtIndex:i] and:scrollView.frame.size.width * i]];
}
}
- (UIImageView *)guideImage:(NSString*)imageName and:(int)n
{
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(x, 0, self.view.frame.size.width, self.view.frame.size.height)];
[imageView setImage:[UIImage imageNamed:imageName]];
return imageView;
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
int currentpage=self.scrollView.contentOffset.x/self.view.frame.size.width;
NSLog(@"current page==%d",currentpage);
[pageControl setCurrentPage:self.scrollView.contentOffset.x/self.view.frame.size.width];
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
NSLog(@"scroll");
[self.pageControl setCurrentPage:self.scrollView.contentOffset.x/self.view.frame.size.width];
}
@end
0 Comment(s)