How can I change the color of pagination dots of UIPageControl?How to change UIPageControl dotsIs there a way to change page indicator dots colorCustomize dot with image of UIPageControl at index 0 of UIPageControlNo visible @interface for 'NSObject<PageControlDelegate>' declares the selector 'pageControlPageDidChange:'How to change the color of pagination dots in UIPageControl with a different color per pagepagecontrol indicator custom image instead of DefaultChanging the colour of UIPageControl dots in MonoTouchpagecontrol selectable page visibility color?Alternative way to load ViewControllers on a UIPageControlHow to set only layer.border-color for UIpage control dots in swiftHow can I develop for iPhone using a Windows development machine?How to change the name of an iOS app?UITableView - change section header coloruipagecontrol indicator(dot)issueCustom UIPageControl dots color not changingchange the interspace between UIPageControl dotsHow to change Status Bar text color in iOSHow can I change image tintColor in iOS and WatchKitUIPageControl dots with larger space in between each dotsHow to change the color of pagination dots in UIPageControl with a different color per page
How do I implement a file system driver driver in Linux?
Fly on a jet pack vs fly with a jet pack?
Is possible to search in vim history?
Translation of Scottish 16th century church stained glass
Ridge Regression with Gradient Descent Converges to OLS estimates
What linear sensor for a keyboard?
Did US corporations pay demonstrators in the German demonstrations against article 13?
Is it improper etiquette to ask your opponent what his/her rating is before the game?
Why does Async/Await work properly when the loop is inside the async function and not the other way around?
Why did the EU agree to delay the Brexit deadline?
A Permanent Norse Presence in America
Did arcade monitors have same pixel aspect ratio as TV sets?
Can the Supreme Court overturn an impeachment?
Drawing ramified coverings with tikz
Greco-Roman egalitarianism
Can someone explain how this makes sense electrically?
Is it possible to use .desktop files to open local pdf files on specific pages with a browser?
How can "mimic phobia" be cured or prevented?
Melting point of aspirin, contradicting sources
How to align and center standalone amsmath equations?
Are lightweight LN wallets vulnerable to transaction withholding?
Should I install hardwood flooring or cabinets first?
Greatest common substring
We have a love-hate relationship
How can I change the color of pagination dots of UIPageControl?
How to change UIPageControl dotsIs there a way to change page indicator dots colorCustomize dot with image of UIPageControl at index 0 of UIPageControlNo visible @interface for 'NSObject<PageControlDelegate>' declares the selector 'pageControlPageDidChange:'How to change the color of pagination dots in UIPageControl with a different color per pagepagecontrol indicator custom image instead of DefaultChanging the colour of UIPageControl dots in MonoTouchpagecontrol selectable page visibility color?Alternative way to load ViewControllers on a UIPageControlHow to set only layer.border-color for UIpage control dots in swiftHow can I develop for iPhone using a Windows development machine?How to change the name of an iOS app?UITableView - change section header coloruipagecontrol indicator(dot)issueCustom UIPageControl dots color not changingchange the interspace between UIPageControl dotsHow to change Status Bar text color in iOSHow can I change image tintColor in iOS and WatchKitUIPageControl dots with larger space in between each dotsHow to change the color of pagination dots in UIPageControl with a different color per page
I am developing an application in which I want to change either color or image of UIPageControl
pagination dots. How can I change it? Is it possible to customize UIpageControl
on above scenario?
iphone ios uipagecontrol
add a comment |
I am developing an application in which I want to change either color or image of UIPageControl
pagination dots. How can I change it? Is it possible to customize UIpageControl
on above scenario?
iphone ios uipagecontrol
add a comment |
I am developing an application in which I want to change either color or image of UIPageControl
pagination dots. How can I change it? Is it possible to customize UIpageControl
on above scenario?
iphone ios uipagecontrol
I am developing an application in which I want to change either color or image of UIPageControl
pagination dots. How can I change it? Is it possible to customize UIpageControl
on above scenario?
iphone ios uipagecontrol
iphone ios uipagecontrol
edited Mar 8 at 6:39
Cœur
19.1k9114155
19.1k9114155
asked May 31 '10 at 9:33
TirthTirth
5,41094881
5,41094881
add a comment |
add a comment |
18 Answers
18
active
oldest
votes
UPDATE:
This answer is 6 years old and very outdated, but it's still attracting votes and comments. Ever since iOS 6.0 you should be using the pageIndicatorTintColor
and currentPageIndicatorTintColor
properties on UIPageControl
.
ORIGINAL ANSWER:
I ran into this problem today and decided to write my own simple replacement class.
It's a sublassed UIView that uses Core Graphics to render the dots in the colors you specify.
You use the exposed properties to customize and control it.
If you want to you can register a delegate object to get notifications when the user taps on one of the little page dots. If no delegate is registered then the view will not react to touch input.
It's completely fresh from the oven, but seems to work. Let me know if you run into any problems with it.
Future improvements:
- Resize the dots to fit the current
bounds if there are too many. - Don't redraw the entire view in drawRect:
Example use:
CGRect f = CGRectMake(0, 0, 320, 20);
PageControl *pageControl = [[[PageControl alloc] initWithFrame:f] autorelease];
pageControl.numberOfPages = 10;
pageControl.currentPage = 5;
pageControl.delegate = self;
[self addSubview:pageControl];
Header file:
//
// PageControl.h
//
// Replacement for UIPageControl because that one only supports white dots.
//
// Created by Morten Heiberg <morten@heiberg.net> on November 1, 2010.
//
#import <UIKit/UIKit.h>
@protocol PageControlDelegate;
@interface PageControl : UIView
@private
NSInteger _currentPage;
NSInteger _numberOfPages;
UIColor *dotColorCurrentPage;
UIColor *dotColorOtherPage;
NSObject<PageControlDelegate> *delegate;
//If ARC use __unsafe_unretained id delegate;
// Set these to control the PageControl.
@property (nonatomic) NSInteger currentPage;
@property (nonatomic) NSInteger numberOfPages;
// Customize these as well as the backgroundColor property.
@property (nonatomic, retain) UIColor *dotColorCurrentPage;
@property (nonatomic, retain) UIColor *dotColorOtherPage;
// Optional delegate for callbacks when user taps a page dot.
@property (nonatomic, retain) NSObject<PageControlDelegate> *delegate;
@end
@protocol PageControlDelegate<NSObject>
@optional
- (void)pageControlPageDidChange:(PageControl *)pageControl;
@end
Implementation file:
//
// PageControl.m
//
// Replacement for UIPageControl because that one only supports white dots.
//
// Created by Morten Heiberg <morten@heiberg.net> on November 1, 2010.
//
#import "PageControl.h"
// Tweak these or make them dynamic.
#define kDotDiameter 7.0
#define kDotSpacer 7.0
@implementation PageControl
@synthesize dotColorCurrentPage;
@synthesize dotColorOtherPage;
@synthesize delegate;
- (NSInteger)currentPage
return _currentPage;
- (void)setCurrentPage:(NSInteger)page
_currentPage = MIN(MAX(0, page), _numberOfPages-1);
[self setNeedsDisplay];
- (NSInteger)numberOfPages
return _numberOfPages;
- (void)setNumberOfPages:(NSInteger)pages
_numberOfPages = MAX(0, pages);
_currentPage = MIN(MAX(0, _currentPage), _numberOfPages-1);
[self setNeedsDisplay];
- (id)initWithFrame:(CGRect)frame
if ((self = [super initWithFrame:frame]))
// Default colors.
self.backgroundColor = [UIColor clearColor];
self.dotColorCurrentPage = [UIColor blackColor];
self.dotColorOtherPage = [UIColor lightGrayColor];
UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipedRight:)];
[swipeRight setDirection:UISwipeGestureRecognizerDirectionRight];
[self addGestureRecognizer:swipeRight];
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipedLeft:)];
[swipe setDirection:UISwipeGestureRecognizerDirectionLeft];
[self addGestureRecognizer:swipe];
return self;
-(void) swipedLeft:(UISwipeGestureRecognizer *) recognizer
self.currentPage++;
-(void) swipedRight:(UISwipeGestureRecognizer *) recognizer
self.currentPage--;
- (void)drawRect:(CGRect)rect
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetAllowsAntialiasing(context, true);
CGRect currentBounds = self.bounds;
CGFloat dotsWidth = self.numberOfPages*kDotDiameter + MAX(0, self.numberOfPages-1)*kDotSpacer;
CGFloat x = CGRectGetMidX(currentBounds)-dotsWidth/2;
CGFloat y = CGRectGetMidY(currentBounds)-kDotDiameter/2;
for (int i=0; i<_numberOfPages; i++)
CGRect circleRect = CGRectMake(x, y, kDotDiameter, kDotDiameter);
if (i == _currentPage)
CGContextSetFillColorWithColor(context, self.dotColorCurrentPage.CGColor);
else
CGContextSetFillColorWithColor(context, self.dotColorOtherPage.CGColor);
CGContextFillEllipseInRect(context, circleRect);
x += kDotDiameter + kDotSpacer;
- (void)dealloc
[dotColorCurrentPage release];
[dotColorOtherPage release];
[delegate release];
[super dealloc];
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
(x>dotSpanX)
@end
So how does this work? I'm using pagecontrolPageDidChange method and I'm not getting anything. I can't click any of the buttons
– Adam
Sep 5 '11 at 17:35
HI Heiberg, i used this to change my page of scrollview, how do you do it from your code ? [pageControl1 addTarget:self action:@selector(changePage:) forControlEvents:UIControlEventValueChanged];
– Desmond
Nov 3 '11 at 12:01
//Action for page changing on UIPageControl -(void)changePage:(UIPageControl*)control //int page = pageControl.currentPage; int page = pageControl.currentPage; // update the scroll view to the appropriate page CGRect frame = scrollview.frame; frame.origin.x = frame.size.width * page; frame.origin.y = 0; [scrollview scrollRectToVisible:frame animated:YES]; pageControlUsed = YES;
– Desmond
Nov 3 '11 at 12:04
For running this code with ARC you'll simply need to remove the dealloc method, change assign to weak and add a __weak before the concerned property declaration. Very nice. Thanks a lot.
– cschuff
Dec 1 '11 at 16:17
why don't you write a tutorial on it, buddy ???
– Harshit Gupta
Jan 1 '13 at 7:08
|
show 2 more comments
In iOS 6 you can set the tint color of UIPageControl
:
There are 2 new properties:
pageIndicatorTintColor
currentPageIndicatorTintColor
You can also use the appearance API to change the tint color of all page indicators.
If you are targeting iOS 5 make sure it doesn't crash:
if ([pageControl respondsToSelector:@selector(setPageIndicatorTintColor:)])
pageControl.pageIndicatorTintColor = [UIColor whiteColor];
What about iOS 5? How do you make sure this doesn't crash?
– jjxtra
Apr 21 '13 at 19:42
@PsychoDad I've edited my answer
– Felix
Apr 21 '13 at 20:09
add a comment |
pageControl.pageIndicatorTintColor = [UIColor redColor];
pageControl.currentPageIndicatorTintColor = [UIColor redColor];
works for iOS6
2
this on did the work for me, works sweetly on iOS7.
– Felipe
Jan 17 '14 at 12:22
2
I was bummed that i was going to need to subclass UIPageControl. This did the trick. This should be at position #1.
– Forrest
Apr 2 '14 at 16:55
Why is such a complex answer the top voted one when this is literally all you need?
– TaylorAllred
May 6 '15 at 20:30
add a comment |
In case anyone wants an ARC / modern version of it (no need to redefine properties as ivar, no dealloc, and works with Interface Builder) :
#import <UIKit/UIKit.h>
@protocol PageControlDelegate;
@interface PageControl : UIView
// Set these to control the PageControl.
@property (nonatomic) NSInteger currentPage;
@property (nonatomic) NSInteger numberOfPages;
// Customize these as well as the backgroundColor property.
@property (nonatomic, strong) UIColor *dotColorCurrentPage;
@property (nonatomic, strong) UIColor *dotColorOtherPage;
// Optional delegate for callbacks when user taps a page dot.
@property (nonatomic, weak) NSObject<PageControlDelegate> *delegate;
@end
@protocol PageControlDelegate<NSObject>
@optional
- (void)pageControlPageDidChange:(PageControl *)pageControl;
@end
PageControl.m :
#import "PageControl.h"
// Tweak these or make them dynamic.
#define kDotDiameter 7.0
#define kDotSpacer 7.0
@implementation PageControl
@synthesize dotColorCurrentPage;
@synthesize dotColorOtherPage;
@synthesize currentPage;
@synthesize numberOfPages;
@synthesize delegate;
- (void)setCurrentPage:(NSInteger)page
currentPage = MIN(MAX(0, page), self.numberOfPages-1);
[self setNeedsDisplay];
- (void)setNumberOfPages:(NSInteger)pages
numberOfPages = MAX(0, pages);
currentPage = MIN(MAX(0, self.currentPage), numberOfPages-1);
[self setNeedsDisplay];
- (id)initWithFrame:(CGRect)frame
if (self = [super initWithFrame:frame])
// Default colors.
self.backgroundColor = [UIColor clearColor];
self.dotColorCurrentPage = [UIColor blackColor];
self.dotColorOtherPage = [UIColor lightGrayColor];
return self;
-(id)initWithCoder:(NSCoder *)aDecoder
if (self = [super initWithCoder:aDecoder])
self.dotColorCurrentPage = [UIColor blackColor];
self.dotColorOtherPage = [UIColor lightGrayColor];
return self;
- (void)drawRect:(CGRect)rect
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetAllowsAntialiasing(context, true);
CGRect currentBounds = self.bounds;
CGFloat dotsWidth = self.numberOfPages*kDotDiameter + MAX(0, self.numberOfPages-1)*kDotSpacer;
CGFloat x = CGRectGetMidX(currentBounds)-dotsWidth/2;
CGFloat y = CGRectGetMidY(currentBounds)-kDotDiameter/2;
for (int i=0; i<self.numberOfPages; i++)
CGRect circleRect = CGRectMake(x, y, kDotDiameter, kDotDiameter);
if (i == self.currentPage)
CGContextSetFillColorWithColor(context, self.dotColorCurrentPage.CGColor);
else
CGContextSetFillColorWithColor(context, self.dotColorOtherPage.CGColor);
CGContextFillEllipseInRect(context, circleRect);
x += kDotDiameter + kDotSpacer;
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
(x>dotSpanX)
@end
1
A minor addition to stop it sending to the delegate if the page number didn't actually change after a touch. NSInteger newPage = floor(x/(kDotDiameter+kDotSpacer)); if (self.currentPage == newPage) return;
– theLastNightTrain
Jul 16 '12 at 12:59
add a comment |
The answer provided by Heiberg works really well, however the page control does not behave exactly like the one by apple.
If you want the page control to behave like the one from apple does (always increment the current page by one if you touch the second half, otherwise decrease by one), try this touchesBegan-method instead:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
CGPoint touchPoint = [[[event touchesForView:self] anyObject] locationInView:self];
CGRect currentBounds = self.bounds;
CGFloat x = touchPoint.x - CGRectGetMidX(currentBounds);
if(x<0 && self.currentPage>=0)
self.currentPage--;
[self.delegate pageControlPageDidChange:self];
else if(x>0 && self.currentPage<self.numberOfPages-1)
self.currentPage++;
[self.delegate pageControlPageDidChange:self];
add a comment |
Add the following code to DidFinishLauch in AppDelegate,
UIPageControl *pageControl = [UIPageControl appearance];
pageControl.pageIndicatorTintColor = [UIColor lightGrayColor];
pageControl.currentPageIndicatorTintColor = [UIColor blackColor];
pageControl.backgroundColor = [UIColor whiteColor];
Hope this will help.
add a comment |
use this for coding
if ([pageControl respondsToSelector:@selector(setPageIndicatorTintColor:)])
pageControl.pageIndicatorTintColor = [UIColor whiteColor];
or from storyboard you can change from current page tint
Thanks... keep sharing :)
– Tirth
Sep 7 '15 at 6:01
add a comment |
Adding to existing answers, it can be done like,
add a comment |
In Swift, this code inside the UIPageViewController is getting a reference to the page indicator and setting its properties
override func viewDidLoad()
super.viewDidLoad()
//Creating the proxy
let pageControl = UIPageControl.appearance()
//Customizing
pageControl.pageIndicatorTintColor = UIColor.lightGrayColor()
pageControl.currentPageIndicatorTintColor = UIColor.darkGrayColor()
//Setting the background of the view controller so the dots wont be on a black background
self.view.backgroundColor = UIColor.whiteColor()
UIPageControl
is not the same asUIPageViewController
– jungledev
Jul 12 '18 at 14:02
add a comment |
It's easy with Swift 1.2:
UIPageControl.appearance().pageIndicatorTintColor = UIColor.lightGrayColor()
UIPageControl.appearance().currentPageIndicatorTintColor = UIColor.redColor()
2
This sets it globally. If you have multiple UIPageControls in your app and you need different colors based on class then useUIPageControl.appearanceWhenContainedInInstancesOfClasses([MyClassName.self])
instead ofUIPageControl.appearance()
. Requires iOS 9.
– Jon
Jan 29 '16 at 17:10
add a comment |
You can fix it with ease by adding the following code to your appdelegate.m file in your didFinishLaunchingWithOptions
method:
UIPageControl *pageControl = [UIPageControl appearance];
pageControl.pageIndicatorTintColor = [UIColor darkGrayColor];
pageControl.currentPageIndicatorTintColor = [UIColor orangeColor];
pageControl.backgroundColor = [UIColor whiteColor]
add a comment |
This is worked for me in iOS 7.
pageControl.pageIndicatorTintColor = [UIColor purpleColor];
pageControl.currentPageIndicatorTintColor = [UIColor magentaColor];
add a comment |
It's not possible using the iPhone SDK from an official standpoint. You might be able to do it using private methods, but that will be a barrier to getting onto the app store.
The only other safe solution is to create yout own page control which shpuldnt be too difficult given that the page control simply displays what page is currently shown in a scroll view.
Theere isn't a link to my solution. My solution is there in text just above your comment. Either look for the private methods (I won't know what these are) or write your own (I'm not going to do that for you).
– Jasarien
May 31 '10 at 13:41
add a comment |
@Jasarien I think you can subclass UIPageControll, line picked from apple doc only "Subclasses that customize the appearance of the page control can use this method to resize the page control when the page count changes" for the method sizeForNumberOfPages:
add a comment |
You could also use Three20 Library that contains a styleable PageControl and dozens of other helpful UI Controls and Abstractions.
and the link is: three20.info
– Nir Pengas
Feb 13 '12 at 15:14
add a comment |
In cased of Swift 2.0
and up, the below code will work:
pageControl.pageIndicatorTintColor = UIColor.whiteColor()
pageControl.currentPageIndicatorTintColor = UIColor.redColor()
add a comment |
Here is a Swift 3.0 solution ... you know if you are OK with accepting stated risk: "Modifying the subviews of an existing control is fragile".
You will have to call updateDots() in your viewDidAppear() and your valueChanged handler for the page control.
import UIKit
class CustomImagePageControl: UIPageControl
let activeImage:UIImage = UIImage(named: "SelectedPage")!
let inactiveImage:UIImage = UIImage(named: "UnselectedPage")!
override func awakeFromNib()
super.awakeFromNib()
self.pageIndicatorTintColor = UIColor.clear
self.currentPageIndicatorTintColor = UIColor.clear
self.clipsToBounds = false
func updateDots()
var i = 0
for view in self.subviews
if let imageView = self.imageForSubview(view)
if i == self.currentPage
imageView.image = self.activeImage
else
imageView.image = self.inactiveImage
i = i + 1
else
var dotImage = self.inactiveImage
if i == self.currentPage
dotImage = self.activeImage
view.clipsToBounds = false
view.addSubview(UIImageView(image:dotImage))
i = i + 1
fileprivate func imageForSubview(_ view:UIView) -> UIImageView?
var dot:UIImageView?
if let dotImageView = view as? UIImageView
dot = dotImageView
else
for foundView in view.subviews
if let imageView = foundView as? UIImageView
dot = imageView
break
return dot
add a comment |
myView.superview.tintColor = [UIColor colorWithRed:1.0f
green:1.0f blue:1.0f alpha:1.0f];
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f2942636%2fhow-can-i-change-the-color-of-pagination-dots-of-uipagecontrol%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
18 Answers
18
active
oldest
votes
18 Answers
18
active
oldest
votes
active
oldest
votes
active
oldest
votes
UPDATE:
This answer is 6 years old and very outdated, but it's still attracting votes and comments. Ever since iOS 6.0 you should be using the pageIndicatorTintColor
and currentPageIndicatorTintColor
properties on UIPageControl
.
ORIGINAL ANSWER:
I ran into this problem today and decided to write my own simple replacement class.
It's a sublassed UIView that uses Core Graphics to render the dots in the colors you specify.
You use the exposed properties to customize and control it.
If you want to you can register a delegate object to get notifications when the user taps on one of the little page dots. If no delegate is registered then the view will not react to touch input.
It's completely fresh from the oven, but seems to work. Let me know if you run into any problems with it.
Future improvements:
- Resize the dots to fit the current
bounds if there are too many. - Don't redraw the entire view in drawRect:
Example use:
CGRect f = CGRectMake(0, 0, 320, 20);
PageControl *pageControl = [[[PageControl alloc] initWithFrame:f] autorelease];
pageControl.numberOfPages = 10;
pageControl.currentPage = 5;
pageControl.delegate = self;
[self addSubview:pageControl];
Header file:
//
// PageControl.h
//
// Replacement for UIPageControl because that one only supports white dots.
//
// Created by Morten Heiberg <morten@heiberg.net> on November 1, 2010.
//
#import <UIKit/UIKit.h>
@protocol PageControlDelegate;
@interface PageControl : UIView
@private
NSInteger _currentPage;
NSInteger _numberOfPages;
UIColor *dotColorCurrentPage;
UIColor *dotColorOtherPage;
NSObject<PageControlDelegate> *delegate;
//If ARC use __unsafe_unretained id delegate;
// Set these to control the PageControl.
@property (nonatomic) NSInteger currentPage;
@property (nonatomic) NSInteger numberOfPages;
// Customize these as well as the backgroundColor property.
@property (nonatomic, retain) UIColor *dotColorCurrentPage;
@property (nonatomic, retain) UIColor *dotColorOtherPage;
// Optional delegate for callbacks when user taps a page dot.
@property (nonatomic, retain) NSObject<PageControlDelegate> *delegate;
@end
@protocol PageControlDelegate<NSObject>
@optional
- (void)pageControlPageDidChange:(PageControl *)pageControl;
@end
Implementation file:
//
// PageControl.m
//
// Replacement for UIPageControl because that one only supports white dots.
//
// Created by Morten Heiberg <morten@heiberg.net> on November 1, 2010.
//
#import "PageControl.h"
// Tweak these or make them dynamic.
#define kDotDiameter 7.0
#define kDotSpacer 7.0
@implementation PageControl
@synthesize dotColorCurrentPage;
@synthesize dotColorOtherPage;
@synthesize delegate;
- (NSInteger)currentPage
return _currentPage;
- (void)setCurrentPage:(NSInteger)page
_currentPage = MIN(MAX(0, page), _numberOfPages-1);
[self setNeedsDisplay];
- (NSInteger)numberOfPages
return _numberOfPages;
- (void)setNumberOfPages:(NSInteger)pages
_numberOfPages = MAX(0, pages);
_currentPage = MIN(MAX(0, _currentPage), _numberOfPages-1);
[self setNeedsDisplay];
- (id)initWithFrame:(CGRect)frame
if ((self = [super initWithFrame:frame]))
// Default colors.
self.backgroundColor = [UIColor clearColor];
self.dotColorCurrentPage = [UIColor blackColor];
self.dotColorOtherPage = [UIColor lightGrayColor];
UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipedRight:)];
[swipeRight setDirection:UISwipeGestureRecognizerDirectionRight];
[self addGestureRecognizer:swipeRight];
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipedLeft:)];
[swipe setDirection:UISwipeGestureRecognizerDirectionLeft];
[self addGestureRecognizer:swipe];
return self;
-(void) swipedLeft:(UISwipeGestureRecognizer *) recognizer
self.currentPage++;
-(void) swipedRight:(UISwipeGestureRecognizer *) recognizer
self.currentPage--;
- (void)drawRect:(CGRect)rect
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetAllowsAntialiasing(context, true);
CGRect currentBounds = self.bounds;
CGFloat dotsWidth = self.numberOfPages*kDotDiameter + MAX(0, self.numberOfPages-1)*kDotSpacer;
CGFloat x = CGRectGetMidX(currentBounds)-dotsWidth/2;
CGFloat y = CGRectGetMidY(currentBounds)-kDotDiameter/2;
for (int i=0; i<_numberOfPages; i++)
CGRect circleRect = CGRectMake(x, y, kDotDiameter, kDotDiameter);
if (i == _currentPage)
CGContextSetFillColorWithColor(context, self.dotColorCurrentPage.CGColor);
else
CGContextSetFillColorWithColor(context, self.dotColorOtherPage.CGColor);
CGContextFillEllipseInRect(context, circleRect);
x += kDotDiameter + kDotSpacer;
- (void)dealloc
[dotColorCurrentPage release];
[dotColorOtherPage release];
[delegate release];
[super dealloc];
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
(x>dotSpanX)
@end
So how does this work? I'm using pagecontrolPageDidChange method and I'm not getting anything. I can't click any of the buttons
– Adam
Sep 5 '11 at 17:35
HI Heiberg, i used this to change my page of scrollview, how do you do it from your code ? [pageControl1 addTarget:self action:@selector(changePage:) forControlEvents:UIControlEventValueChanged];
– Desmond
Nov 3 '11 at 12:01
//Action for page changing on UIPageControl -(void)changePage:(UIPageControl*)control //int page = pageControl.currentPage; int page = pageControl.currentPage; // update the scroll view to the appropriate page CGRect frame = scrollview.frame; frame.origin.x = frame.size.width * page; frame.origin.y = 0; [scrollview scrollRectToVisible:frame animated:YES]; pageControlUsed = YES;
– Desmond
Nov 3 '11 at 12:04
For running this code with ARC you'll simply need to remove the dealloc method, change assign to weak and add a __weak before the concerned property declaration. Very nice. Thanks a lot.
– cschuff
Dec 1 '11 at 16:17
why don't you write a tutorial on it, buddy ???
– Harshit Gupta
Jan 1 '13 at 7:08
|
show 2 more comments
UPDATE:
This answer is 6 years old and very outdated, but it's still attracting votes and comments. Ever since iOS 6.0 you should be using the pageIndicatorTintColor
and currentPageIndicatorTintColor
properties on UIPageControl
.
ORIGINAL ANSWER:
I ran into this problem today and decided to write my own simple replacement class.
It's a sublassed UIView that uses Core Graphics to render the dots in the colors you specify.
You use the exposed properties to customize and control it.
If you want to you can register a delegate object to get notifications when the user taps on one of the little page dots. If no delegate is registered then the view will not react to touch input.
It's completely fresh from the oven, but seems to work. Let me know if you run into any problems with it.
Future improvements:
- Resize the dots to fit the current
bounds if there are too many. - Don't redraw the entire view in drawRect:
Example use:
CGRect f = CGRectMake(0, 0, 320, 20);
PageControl *pageControl = [[[PageControl alloc] initWithFrame:f] autorelease];
pageControl.numberOfPages = 10;
pageControl.currentPage = 5;
pageControl.delegate = self;
[self addSubview:pageControl];
Header file:
//
// PageControl.h
//
// Replacement for UIPageControl because that one only supports white dots.
//
// Created by Morten Heiberg <morten@heiberg.net> on November 1, 2010.
//
#import <UIKit/UIKit.h>
@protocol PageControlDelegate;
@interface PageControl : UIView
@private
NSInteger _currentPage;
NSInteger _numberOfPages;
UIColor *dotColorCurrentPage;
UIColor *dotColorOtherPage;
NSObject<PageControlDelegate> *delegate;
//If ARC use __unsafe_unretained id delegate;
// Set these to control the PageControl.
@property (nonatomic) NSInteger currentPage;
@property (nonatomic) NSInteger numberOfPages;
// Customize these as well as the backgroundColor property.
@property (nonatomic, retain) UIColor *dotColorCurrentPage;
@property (nonatomic, retain) UIColor *dotColorOtherPage;
// Optional delegate for callbacks when user taps a page dot.
@property (nonatomic, retain) NSObject<PageControlDelegate> *delegate;
@end
@protocol PageControlDelegate<NSObject>
@optional
- (void)pageControlPageDidChange:(PageControl *)pageControl;
@end
Implementation file:
//
// PageControl.m
//
// Replacement for UIPageControl because that one only supports white dots.
//
// Created by Morten Heiberg <morten@heiberg.net> on November 1, 2010.
//
#import "PageControl.h"
// Tweak these or make them dynamic.
#define kDotDiameter 7.0
#define kDotSpacer 7.0
@implementation PageControl
@synthesize dotColorCurrentPage;
@synthesize dotColorOtherPage;
@synthesize delegate;
- (NSInteger)currentPage
return _currentPage;
- (void)setCurrentPage:(NSInteger)page
_currentPage = MIN(MAX(0, page), _numberOfPages-1);
[self setNeedsDisplay];
- (NSInteger)numberOfPages
return _numberOfPages;
- (void)setNumberOfPages:(NSInteger)pages
_numberOfPages = MAX(0, pages);
_currentPage = MIN(MAX(0, _currentPage), _numberOfPages-1);
[self setNeedsDisplay];
- (id)initWithFrame:(CGRect)frame
if ((self = [super initWithFrame:frame]))
// Default colors.
self.backgroundColor = [UIColor clearColor];
self.dotColorCurrentPage = [UIColor blackColor];
self.dotColorOtherPage = [UIColor lightGrayColor];
UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipedRight:)];
[swipeRight setDirection:UISwipeGestureRecognizerDirectionRight];
[self addGestureRecognizer:swipeRight];
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipedLeft:)];
[swipe setDirection:UISwipeGestureRecognizerDirectionLeft];
[self addGestureRecognizer:swipe];
return self;
-(void) swipedLeft:(UISwipeGestureRecognizer *) recognizer
self.currentPage++;
-(void) swipedRight:(UISwipeGestureRecognizer *) recognizer
self.currentPage--;
- (void)drawRect:(CGRect)rect
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetAllowsAntialiasing(context, true);
CGRect currentBounds = self.bounds;
CGFloat dotsWidth = self.numberOfPages*kDotDiameter + MAX(0, self.numberOfPages-1)*kDotSpacer;
CGFloat x = CGRectGetMidX(currentBounds)-dotsWidth/2;
CGFloat y = CGRectGetMidY(currentBounds)-kDotDiameter/2;
for (int i=0; i<_numberOfPages; i++)
CGRect circleRect = CGRectMake(x, y, kDotDiameter, kDotDiameter);
if (i == _currentPage)
CGContextSetFillColorWithColor(context, self.dotColorCurrentPage.CGColor);
else
CGContextSetFillColorWithColor(context, self.dotColorOtherPage.CGColor);
CGContextFillEllipseInRect(context, circleRect);
x += kDotDiameter + kDotSpacer;
- (void)dealloc
[dotColorCurrentPage release];
[dotColorOtherPage release];
[delegate release];
[super dealloc];
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
(x>dotSpanX)
@end
So how does this work? I'm using pagecontrolPageDidChange method and I'm not getting anything. I can't click any of the buttons
– Adam
Sep 5 '11 at 17:35
HI Heiberg, i used this to change my page of scrollview, how do you do it from your code ? [pageControl1 addTarget:self action:@selector(changePage:) forControlEvents:UIControlEventValueChanged];
– Desmond
Nov 3 '11 at 12:01
//Action for page changing on UIPageControl -(void)changePage:(UIPageControl*)control //int page = pageControl.currentPage; int page = pageControl.currentPage; // update the scroll view to the appropriate page CGRect frame = scrollview.frame; frame.origin.x = frame.size.width * page; frame.origin.y = 0; [scrollview scrollRectToVisible:frame animated:YES]; pageControlUsed = YES;
– Desmond
Nov 3 '11 at 12:04
For running this code with ARC you'll simply need to remove the dealloc method, change assign to weak and add a __weak before the concerned property declaration. Very nice. Thanks a lot.
– cschuff
Dec 1 '11 at 16:17
why don't you write a tutorial on it, buddy ???
– Harshit Gupta
Jan 1 '13 at 7:08
|
show 2 more comments
UPDATE:
This answer is 6 years old and very outdated, but it's still attracting votes and comments. Ever since iOS 6.0 you should be using the pageIndicatorTintColor
and currentPageIndicatorTintColor
properties on UIPageControl
.
ORIGINAL ANSWER:
I ran into this problem today and decided to write my own simple replacement class.
It's a sublassed UIView that uses Core Graphics to render the dots in the colors you specify.
You use the exposed properties to customize and control it.
If you want to you can register a delegate object to get notifications when the user taps on one of the little page dots. If no delegate is registered then the view will not react to touch input.
It's completely fresh from the oven, but seems to work. Let me know if you run into any problems with it.
Future improvements:
- Resize the dots to fit the current
bounds if there are too many. - Don't redraw the entire view in drawRect:
Example use:
CGRect f = CGRectMake(0, 0, 320, 20);
PageControl *pageControl = [[[PageControl alloc] initWithFrame:f] autorelease];
pageControl.numberOfPages = 10;
pageControl.currentPage = 5;
pageControl.delegate = self;
[self addSubview:pageControl];
Header file:
//
// PageControl.h
//
// Replacement for UIPageControl because that one only supports white dots.
//
// Created by Morten Heiberg <morten@heiberg.net> on November 1, 2010.
//
#import <UIKit/UIKit.h>
@protocol PageControlDelegate;
@interface PageControl : UIView
@private
NSInteger _currentPage;
NSInteger _numberOfPages;
UIColor *dotColorCurrentPage;
UIColor *dotColorOtherPage;
NSObject<PageControlDelegate> *delegate;
//If ARC use __unsafe_unretained id delegate;
// Set these to control the PageControl.
@property (nonatomic) NSInteger currentPage;
@property (nonatomic) NSInteger numberOfPages;
// Customize these as well as the backgroundColor property.
@property (nonatomic, retain) UIColor *dotColorCurrentPage;
@property (nonatomic, retain) UIColor *dotColorOtherPage;
// Optional delegate for callbacks when user taps a page dot.
@property (nonatomic, retain) NSObject<PageControlDelegate> *delegate;
@end
@protocol PageControlDelegate<NSObject>
@optional
- (void)pageControlPageDidChange:(PageControl *)pageControl;
@end
Implementation file:
//
// PageControl.m
//
// Replacement for UIPageControl because that one only supports white dots.
//
// Created by Morten Heiberg <morten@heiberg.net> on November 1, 2010.
//
#import "PageControl.h"
// Tweak these or make them dynamic.
#define kDotDiameter 7.0
#define kDotSpacer 7.0
@implementation PageControl
@synthesize dotColorCurrentPage;
@synthesize dotColorOtherPage;
@synthesize delegate;
- (NSInteger)currentPage
return _currentPage;
- (void)setCurrentPage:(NSInteger)page
_currentPage = MIN(MAX(0, page), _numberOfPages-1);
[self setNeedsDisplay];
- (NSInteger)numberOfPages
return _numberOfPages;
- (void)setNumberOfPages:(NSInteger)pages
_numberOfPages = MAX(0, pages);
_currentPage = MIN(MAX(0, _currentPage), _numberOfPages-1);
[self setNeedsDisplay];
- (id)initWithFrame:(CGRect)frame
if ((self = [super initWithFrame:frame]))
// Default colors.
self.backgroundColor = [UIColor clearColor];
self.dotColorCurrentPage = [UIColor blackColor];
self.dotColorOtherPage = [UIColor lightGrayColor];
UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipedRight:)];
[swipeRight setDirection:UISwipeGestureRecognizerDirectionRight];
[self addGestureRecognizer:swipeRight];
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipedLeft:)];
[swipe setDirection:UISwipeGestureRecognizerDirectionLeft];
[self addGestureRecognizer:swipe];
return self;
-(void) swipedLeft:(UISwipeGestureRecognizer *) recognizer
self.currentPage++;
-(void) swipedRight:(UISwipeGestureRecognizer *) recognizer
self.currentPage--;
- (void)drawRect:(CGRect)rect
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetAllowsAntialiasing(context, true);
CGRect currentBounds = self.bounds;
CGFloat dotsWidth = self.numberOfPages*kDotDiameter + MAX(0, self.numberOfPages-1)*kDotSpacer;
CGFloat x = CGRectGetMidX(currentBounds)-dotsWidth/2;
CGFloat y = CGRectGetMidY(currentBounds)-kDotDiameter/2;
for (int i=0; i<_numberOfPages; i++)
CGRect circleRect = CGRectMake(x, y, kDotDiameter, kDotDiameter);
if (i == _currentPage)
CGContextSetFillColorWithColor(context, self.dotColorCurrentPage.CGColor);
else
CGContextSetFillColorWithColor(context, self.dotColorOtherPage.CGColor);
CGContextFillEllipseInRect(context, circleRect);
x += kDotDiameter + kDotSpacer;
- (void)dealloc
[dotColorCurrentPage release];
[dotColorOtherPage release];
[delegate release];
[super dealloc];
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
(x>dotSpanX)
@end
UPDATE:
This answer is 6 years old and very outdated, but it's still attracting votes and comments. Ever since iOS 6.0 you should be using the pageIndicatorTintColor
and currentPageIndicatorTintColor
properties on UIPageControl
.
ORIGINAL ANSWER:
I ran into this problem today and decided to write my own simple replacement class.
It's a sublassed UIView that uses Core Graphics to render the dots in the colors you specify.
You use the exposed properties to customize and control it.
If you want to you can register a delegate object to get notifications when the user taps on one of the little page dots. If no delegate is registered then the view will not react to touch input.
It's completely fresh from the oven, but seems to work. Let me know if you run into any problems with it.
Future improvements:
- Resize the dots to fit the current
bounds if there are too many. - Don't redraw the entire view in drawRect:
Example use:
CGRect f = CGRectMake(0, 0, 320, 20);
PageControl *pageControl = [[[PageControl alloc] initWithFrame:f] autorelease];
pageControl.numberOfPages = 10;
pageControl.currentPage = 5;
pageControl.delegate = self;
[self addSubview:pageControl];
Header file:
//
// PageControl.h
//
// Replacement for UIPageControl because that one only supports white dots.
//
// Created by Morten Heiberg <morten@heiberg.net> on November 1, 2010.
//
#import <UIKit/UIKit.h>
@protocol PageControlDelegate;
@interface PageControl : UIView
@private
NSInteger _currentPage;
NSInteger _numberOfPages;
UIColor *dotColorCurrentPage;
UIColor *dotColorOtherPage;
NSObject<PageControlDelegate> *delegate;
//If ARC use __unsafe_unretained id delegate;
// Set these to control the PageControl.
@property (nonatomic) NSInteger currentPage;
@property (nonatomic) NSInteger numberOfPages;
// Customize these as well as the backgroundColor property.
@property (nonatomic, retain) UIColor *dotColorCurrentPage;
@property (nonatomic, retain) UIColor *dotColorOtherPage;
// Optional delegate for callbacks when user taps a page dot.
@property (nonatomic, retain) NSObject<PageControlDelegate> *delegate;
@end
@protocol PageControlDelegate<NSObject>
@optional
- (void)pageControlPageDidChange:(PageControl *)pageControl;
@end
Implementation file:
//
// PageControl.m
//
// Replacement for UIPageControl because that one only supports white dots.
//
// Created by Morten Heiberg <morten@heiberg.net> on November 1, 2010.
//
#import "PageControl.h"
// Tweak these or make them dynamic.
#define kDotDiameter 7.0
#define kDotSpacer 7.0
@implementation PageControl
@synthesize dotColorCurrentPage;
@synthesize dotColorOtherPage;
@synthesize delegate;
- (NSInteger)currentPage
return _currentPage;
- (void)setCurrentPage:(NSInteger)page
_currentPage = MIN(MAX(0, page), _numberOfPages-1);
[self setNeedsDisplay];
- (NSInteger)numberOfPages
return _numberOfPages;
- (void)setNumberOfPages:(NSInteger)pages
_numberOfPages = MAX(0, pages);
_currentPage = MIN(MAX(0, _currentPage), _numberOfPages-1);
[self setNeedsDisplay];
- (id)initWithFrame:(CGRect)frame
if ((self = [super initWithFrame:frame]))
// Default colors.
self.backgroundColor = [UIColor clearColor];
self.dotColorCurrentPage = [UIColor blackColor];
self.dotColorOtherPage = [UIColor lightGrayColor];
UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipedRight:)];
[swipeRight setDirection:UISwipeGestureRecognizerDirectionRight];
[self addGestureRecognizer:swipeRight];
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipedLeft:)];
[swipe setDirection:UISwipeGestureRecognizerDirectionLeft];
[self addGestureRecognizer:swipe];
return self;
-(void) swipedLeft:(UISwipeGestureRecognizer *) recognizer
self.currentPage++;
-(void) swipedRight:(UISwipeGestureRecognizer *) recognizer
self.currentPage--;
- (void)drawRect:(CGRect)rect
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetAllowsAntialiasing(context, true);
CGRect currentBounds = self.bounds;
CGFloat dotsWidth = self.numberOfPages*kDotDiameter + MAX(0, self.numberOfPages-1)*kDotSpacer;
CGFloat x = CGRectGetMidX(currentBounds)-dotsWidth/2;
CGFloat y = CGRectGetMidY(currentBounds)-kDotDiameter/2;
for (int i=0; i<_numberOfPages; i++)
CGRect circleRect = CGRectMake(x, y, kDotDiameter, kDotDiameter);
if (i == _currentPage)
CGContextSetFillColorWithColor(context, self.dotColorCurrentPage.CGColor);
else
CGContextSetFillColorWithColor(context, self.dotColorOtherPage.CGColor);
CGContextFillEllipseInRect(context, circleRect);
x += kDotDiameter + kDotSpacer;
- (void)dealloc
[dotColorCurrentPage release];
[dotColorOtherPage release];
[delegate release];
[super dealloc];
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
(x>dotSpanX)
@end
edited Mar 3 '16 at 13:50
answered Nov 1 '10 at 22:22
HeibergHeiberg
5,11722128
5,11722128
So how does this work? I'm using pagecontrolPageDidChange method and I'm not getting anything. I can't click any of the buttons
– Adam
Sep 5 '11 at 17:35
HI Heiberg, i used this to change my page of scrollview, how do you do it from your code ? [pageControl1 addTarget:self action:@selector(changePage:) forControlEvents:UIControlEventValueChanged];
– Desmond
Nov 3 '11 at 12:01
//Action for page changing on UIPageControl -(void)changePage:(UIPageControl*)control //int page = pageControl.currentPage; int page = pageControl.currentPage; // update the scroll view to the appropriate page CGRect frame = scrollview.frame; frame.origin.x = frame.size.width * page; frame.origin.y = 0; [scrollview scrollRectToVisible:frame animated:YES]; pageControlUsed = YES;
– Desmond
Nov 3 '11 at 12:04
For running this code with ARC you'll simply need to remove the dealloc method, change assign to weak and add a __weak before the concerned property declaration. Very nice. Thanks a lot.
– cschuff
Dec 1 '11 at 16:17
why don't you write a tutorial on it, buddy ???
– Harshit Gupta
Jan 1 '13 at 7:08
|
show 2 more comments
So how does this work? I'm using pagecontrolPageDidChange method and I'm not getting anything. I can't click any of the buttons
– Adam
Sep 5 '11 at 17:35
HI Heiberg, i used this to change my page of scrollview, how do you do it from your code ? [pageControl1 addTarget:self action:@selector(changePage:) forControlEvents:UIControlEventValueChanged];
– Desmond
Nov 3 '11 at 12:01
//Action for page changing on UIPageControl -(void)changePage:(UIPageControl*)control //int page = pageControl.currentPage; int page = pageControl.currentPage; // update the scroll view to the appropriate page CGRect frame = scrollview.frame; frame.origin.x = frame.size.width * page; frame.origin.y = 0; [scrollview scrollRectToVisible:frame animated:YES]; pageControlUsed = YES;
– Desmond
Nov 3 '11 at 12:04
For running this code with ARC you'll simply need to remove the dealloc method, change assign to weak and add a __weak before the concerned property declaration. Very nice. Thanks a lot.
– cschuff
Dec 1 '11 at 16:17
why don't you write a tutorial on it, buddy ???
– Harshit Gupta
Jan 1 '13 at 7:08
So how does this work? I'm using pagecontrolPageDidChange method and I'm not getting anything. I can't click any of the buttons
– Adam
Sep 5 '11 at 17:35
So how does this work? I'm using pagecontrolPageDidChange method and I'm not getting anything. I can't click any of the buttons
– Adam
Sep 5 '11 at 17:35
HI Heiberg, i used this to change my page of scrollview, how do you do it from your code ? [pageControl1 addTarget:self action:@selector(changePage:) forControlEvents:UIControlEventValueChanged];
– Desmond
Nov 3 '11 at 12:01
HI Heiberg, i used this to change my page of scrollview, how do you do it from your code ? [pageControl1 addTarget:self action:@selector(changePage:) forControlEvents:UIControlEventValueChanged];
– Desmond
Nov 3 '11 at 12:01
//Action for page changing on UIPageControl -(void)changePage:(UIPageControl*)control //int page = pageControl.currentPage; int page = pageControl.currentPage; // update the scroll view to the appropriate page CGRect frame = scrollview.frame; frame.origin.x = frame.size.width * page; frame.origin.y = 0; [scrollview scrollRectToVisible:frame animated:YES]; pageControlUsed = YES;
– Desmond
Nov 3 '11 at 12:04
//Action for page changing on UIPageControl -(void)changePage:(UIPageControl*)control //int page = pageControl.currentPage; int page = pageControl.currentPage; // update the scroll view to the appropriate page CGRect frame = scrollview.frame; frame.origin.x = frame.size.width * page; frame.origin.y = 0; [scrollview scrollRectToVisible:frame animated:YES]; pageControlUsed = YES;
– Desmond
Nov 3 '11 at 12:04
For running this code with ARC you'll simply need to remove the dealloc method, change assign to weak and add a __weak before the concerned property declaration. Very nice. Thanks a lot.
– cschuff
Dec 1 '11 at 16:17
For running this code with ARC you'll simply need to remove the dealloc method, change assign to weak and add a __weak before the concerned property declaration. Very nice. Thanks a lot.
– cschuff
Dec 1 '11 at 16:17
why don't you write a tutorial on it, buddy ???
– Harshit Gupta
Jan 1 '13 at 7:08
why don't you write a tutorial on it, buddy ???
– Harshit Gupta
Jan 1 '13 at 7:08
|
show 2 more comments
In iOS 6 you can set the tint color of UIPageControl
:
There are 2 new properties:
pageIndicatorTintColor
currentPageIndicatorTintColor
You can also use the appearance API to change the tint color of all page indicators.
If you are targeting iOS 5 make sure it doesn't crash:
if ([pageControl respondsToSelector:@selector(setPageIndicatorTintColor:)])
pageControl.pageIndicatorTintColor = [UIColor whiteColor];
What about iOS 5? How do you make sure this doesn't crash?
– jjxtra
Apr 21 '13 at 19:42
@PsychoDad I've edited my answer
– Felix
Apr 21 '13 at 20:09
add a comment |
In iOS 6 you can set the tint color of UIPageControl
:
There are 2 new properties:
pageIndicatorTintColor
currentPageIndicatorTintColor
You can also use the appearance API to change the tint color of all page indicators.
If you are targeting iOS 5 make sure it doesn't crash:
if ([pageControl respondsToSelector:@selector(setPageIndicatorTintColor:)])
pageControl.pageIndicatorTintColor = [UIColor whiteColor];
What about iOS 5? How do you make sure this doesn't crash?
– jjxtra
Apr 21 '13 at 19:42
@PsychoDad I've edited my answer
– Felix
Apr 21 '13 at 20:09
add a comment |
In iOS 6 you can set the tint color of UIPageControl
:
There are 2 new properties:
pageIndicatorTintColor
currentPageIndicatorTintColor
You can also use the appearance API to change the tint color of all page indicators.
If you are targeting iOS 5 make sure it doesn't crash:
if ([pageControl respondsToSelector:@selector(setPageIndicatorTintColor:)])
pageControl.pageIndicatorTintColor = [UIColor whiteColor];
In iOS 6 you can set the tint color of UIPageControl
:
There are 2 new properties:
pageIndicatorTintColor
currentPageIndicatorTintColor
You can also use the appearance API to change the tint color of all page indicators.
If you are targeting iOS 5 make sure it doesn't crash:
if ([pageControl respondsToSelector:@selector(setPageIndicatorTintColor:)])
pageControl.pageIndicatorTintColor = [UIColor whiteColor];
edited May 8 '13 at 15:31
Matthias Bauch
83.6k15206242
83.6k15206242
answered Jun 22 '12 at 10:37
FelixFelix
33.9k1187139
33.9k1187139
What about iOS 5? How do you make sure this doesn't crash?
– jjxtra
Apr 21 '13 at 19:42
@PsychoDad I've edited my answer
– Felix
Apr 21 '13 at 20:09
add a comment |
What about iOS 5? How do you make sure this doesn't crash?
– jjxtra
Apr 21 '13 at 19:42
@PsychoDad I've edited my answer
– Felix
Apr 21 '13 at 20:09
What about iOS 5? How do you make sure this doesn't crash?
– jjxtra
Apr 21 '13 at 19:42
What about iOS 5? How do you make sure this doesn't crash?
– jjxtra
Apr 21 '13 at 19:42
@PsychoDad I've edited my answer
– Felix
Apr 21 '13 at 20:09
@PsychoDad I've edited my answer
– Felix
Apr 21 '13 at 20:09
add a comment |
pageControl.pageIndicatorTintColor = [UIColor redColor];
pageControl.currentPageIndicatorTintColor = [UIColor redColor];
works for iOS6
2
this on did the work for me, works sweetly on iOS7.
– Felipe
Jan 17 '14 at 12:22
2
I was bummed that i was going to need to subclass UIPageControl. This did the trick. This should be at position #1.
– Forrest
Apr 2 '14 at 16:55
Why is such a complex answer the top voted one when this is literally all you need?
– TaylorAllred
May 6 '15 at 20:30
add a comment |
pageControl.pageIndicatorTintColor = [UIColor redColor];
pageControl.currentPageIndicatorTintColor = [UIColor redColor];
works for iOS6
2
this on did the work for me, works sweetly on iOS7.
– Felipe
Jan 17 '14 at 12:22
2
I was bummed that i was going to need to subclass UIPageControl. This did the trick. This should be at position #1.
– Forrest
Apr 2 '14 at 16:55
Why is such a complex answer the top voted one when this is literally all you need?
– TaylorAllred
May 6 '15 at 20:30
add a comment |
pageControl.pageIndicatorTintColor = [UIColor redColor];
pageControl.currentPageIndicatorTintColor = [UIColor redColor];
works for iOS6
pageControl.pageIndicatorTintColor = [UIColor redColor];
pageControl.currentPageIndicatorTintColor = [UIColor redColor];
works for iOS6
edited Feb 19 '14 at 11:51
user1349663
3461418
3461418
answered Oct 3 '13 at 20:38
Add080bbAAdd080bbA
9161921
9161921
2
this on did the work for me, works sweetly on iOS7.
– Felipe
Jan 17 '14 at 12:22
2
I was bummed that i was going to need to subclass UIPageControl. This did the trick. This should be at position #1.
– Forrest
Apr 2 '14 at 16:55
Why is such a complex answer the top voted one when this is literally all you need?
– TaylorAllred
May 6 '15 at 20:30
add a comment |
2
this on did the work for me, works sweetly on iOS7.
– Felipe
Jan 17 '14 at 12:22
2
I was bummed that i was going to need to subclass UIPageControl. This did the trick. This should be at position #1.
– Forrest
Apr 2 '14 at 16:55
Why is such a complex answer the top voted one when this is literally all you need?
– TaylorAllred
May 6 '15 at 20:30
2
2
this on did the work for me, works sweetly on iOS7.
– Felipe
Jan 17 '14 at 12:22
this on did the work for me, works sweetly on iOS7.
– Felipe
Jan 17 '14 at 12:22
2
2
I was bummed that i was going to need to subclass UIPageControl. This did the trick. This should be at position #1.
– Forrest
Apr 2 '14 at 16:55
I was bummed that i was going to need to subclass UIPageControl. This did the trick. This should be at position #1.
– Forrest
Apr 2 '14 at 16:55
Why is such a complex answer the top voted one when this is literally all you need?
– TaylorAllred
May 6 '15 at 20:30
Why is such a complex answer the top voted one when this is literally all you need?
– TaylorAllred
May 6 '15 at 20:30
add a comment |
In case anyone wants an ARC / modern version of it (no need to redefine properties as ivar, no dealloc, and works with Interface Builder) :
#import <UIKit/UIKit.h>
@protocol PageControlDelegate;
@interface PageControl : UIView
// Set these to control the PageControl.
@property (nonatomic) NSInteger currentPage;
@property (nonatomic) NSInteger numberOfPages;
// Customize these as well as the backgroundColor property.
@property (nonatomic, strong) UIColor *dotColorCurrentPage;
@property (nonatomic, strong) UIColor *dotColorOtherPage;
// Optional delegate for callbacks when user taps a page dot.
@property (nonatomic, weak) NSObject<PageControlDelegate> *delegate;
@end
@protocol PageControlDelegate<NSObject>
@optional
- (void)pageControlPageDidChange:(PageControl *)pageControl;
@end
PageControl.m :
#import "PageControl.h"
// Tweak these or make them dynamic.
#define kDotDiameter 7.0
#define kDotSpacer 7.0
@implementation PageControl
@synthesize dotColorCurrentPage;
@synthesize dotColorOtherPage;
@synthesize currentPage;
@synthesize numberOfPages;
@synthesize delegate;
- (void)setCurrentPage:(NSInteger)page
currentPage = MIN(MAX(0, page), self.numberOfPages-1);
[self setNeedsDisplay];
- (void)setNumberOfPages:(NSInteger)pages
numberOfPages = MAX(0, pages);
currentPage = MIN(MAX(0, self.currentPage), numberOfPages-1);
[self setNeedsDisplay];
- (id)initWithFrame:(CGRect)frame
if (self = [super initWithFrame:frame])
// Default colors.
self.backgroundColor = [UIColor clearColor];
self.dotColorCurrentPage = [UIColor blackColor];
self.dotColorOtherPage = [UIColor lightGrayColor];
return self;
-(id)initWithCoder:(NSCoder *)aDecoder
if (self = [super initWithCoder:aDecoder])
self.dotColorCurrentPage = [UIColor blackColor];
self.dotColorOtherPage = [UIColor lightGrayColor];
return self;
- (void)drawRect:(CGRect)rect
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetAllowsAntialiasing(context, true);
CGRect currentBounds = self.bounds;
CGFloat dotsWidth = self.numberOfPages*kDotDiameter + MAX(0, self.numberOfPages-1)*kDotSpacer;
CGFloat x = CGRectGetMidX(currentBounds)-dotsWidth/2;
CGFloat y = CGRectGetMidY(currentBounds)-kDotDiameter/2;
for (int i=0; i<self.numberOfPages; i++)
CGRect circleRect = CGRectMake(x, y, kDotDiameter, kDotDiameter);
if (i == self.currentPage)
CGContextSetFillColorWithColor(context, self.dotColorCurrentPage.CGColor);
else
CGContextSetFillColorWithColor(context, self.dotColorOtherPage.CGColor);
CGContextFillEllipseInRect(context, circleRect);
x += kDotDiameter + kDotSpacer;
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
(x>dotSpanX)
@end
1
A minor addition to stop it sending to the delegate if the page number didn't actually change after a touch. NSInteger newPage = floor(x/(kDotDiameter+kDotSpacer)); if (self.currentPage == newPage) return;
– theLastNightTrain
Jul 16 '12 at 12:59
add a comment |
In case anyone wants an ARC / modern version of it (no need to redefine properties as ivar, no dealloc, and works with Interface Builder) :
#import <UIKit/UIKit.h>
@protocol PageControlDelegate;
@interface PageControl : UIView
// Set these to control the PageControl.
@property (nonatomic) NSInteger currentPage;
@property (nonatomic) NSInteger numberOfPages;
// Customize these as well as the backgroundColor property.
@property (nonatomic, strong) UIColor *dotColorCurrentPage;
@property (nonatomic, strong) UIColor *dotColorOtherPage;
// Optional delegate for callbacks when user taps a page dot.
@property (nonatomic, weak) NSObject<PageControlDelegate> *delegate;
@end
@protocol PageControlDelegate<NSObject>
@optional
- (void)pageControlPageDidChange:(PageControl *)pageControl;
@end
PageControl.m :
#import "PageControl.h"
// Tweak these or make them dynamic.
#define kDotDiameter 7.0
#define kDotSpacer 7.0
@implementation PageControl
@synthesize dotColorCurrentPage;
@synthesize dotColorOtherPage;
@synthesize currentPage;
@synthesize numberOfPages;
@synthesize delegate;
- (void)setCurrentPage:(NSInteger)page
currentPage = MIN(MAX(0, page), self.numberOfPages-1);
[self setNeedsDisplay];
- (void)setNumberOfPages:(NSInteger)pages
numberOfPages = MAX(0, pages);
currentPage = MIN(MAX(0, self.currentPage), numberOfPages-1);
[self setNeedsDisplay];
- (id)initWithFrame:(CGRect)frame
if (self = [super initWithFrame:frame])
// Default colors.
self.backgroundColor = [UIColor clearColor];
self.dotColorCurrentPage = [UIColor blackColor];
self.dotColorOtherPage = [UIColor lightGrayColor];
return self;
-(id)initWithCoder:(NSCoder *)aDecoder
if (self = [super initWithCoder:aDecoder])
self.dotColorCurrentPage = [UIColor blackColor];
self.dotColorOtherPage = [UIColor lightGrayColor];
return self;
- (void)drawRect:(CGRect)rect
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetAllowsAntialiasing(context, true);
CGRect currentBounds = self.bounds;
CGFloat dotsWidth = self.numberOfPages*kDotDiameter + MAX(0, self.numberOfPages-1)*kDotSpacer;
CGFloat x = CGRectGetMidX(currentBounds)-dotsWidth/2;
CGFloat y = CGRectGetMidY(currentBounds)-kDotDiameter/2;
for (int i=0; i<self.numberOfPages; i++)
CGRect circleRect = CGRectMake(x, y, kDotDiameter, kDotDiameter);
if (i == self.currentPage)
CGContextSetFillColorWithColor(context, self.dotColorCurrentPage.CGColor);
else
CGContextSetFillColorWithColor(context, self.dotColorOtherPage.CGColor);
CGContextFillEllipseInRect(context, circleRect);
x += kDotDiameter + kDotSpacer;
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
(x>dotSpanX)
@end
1
A minor addition to stop it sending to the delegate if the page number didn't actually change after a touch. NSInteger newPage = floor(x/(kDotDiameter+kDotSpacer)); if (self.currentPage == newPage) return;
– theLastNightTrain
Jul 16 '12 at 12:59
add a comment |
In case anyone wants an ARC / modern version of it (no need to redefine properties as ivar, no dealloc, and works with Interface Builder) :
#import <UIKit/UIKit.h>
@protocol PageControlDelegate;
@interface PageControl : UIView
// Set these to control the PageControl.
@property (nonatomic) NSInteger currentPage;
@property (nonatomic) NSInteger numberOfPages;
// Customize these as well as the backgroundColor property.
@property (nonatomic, strong) UIColor *dotColorCurrentPage;
@property (nonatomic, strong) UIColor *dotColorOtherPage;
// Optional delegate for callbacks when user taps a page dot.
@property (nonatomic, weak) NSObject<PageControlDelegate> *delegate;
@end
@protocol PageControlDelegate<NSObject>
@optional
- (void)pageControlPageDidChange:(PageControl *)pageControl;
@end
PageControl.m :
#import "PageControl.h"
// Tweak these or make them dynamic.
#define kDotDiameter 7.0
#define kDotSpacer 7.0
@implementation PageControl
@synthesize dotColorCurrentPage;
@synthesize dotColorOtherPage;
@synthesize currentPage;
@synthesize numberOfPages;
@synthesize delegate;
- (void)setCurrentPage:(NSInteger)page
currentPage = MIN(MAX(0, page), self.numberOfPages-1);
[self setNeedsDisplay];
- (void)setNumberOfPages:(NSInteger)pages
numberOfPages = MAX(0, pages);
currentPage = MIN(MAX(0, self.currentPage), numberOfPages-1);
[self setNeedsDisplay];
- (id)initWithFrame:(CGRect)frame
if (self = [super initWithFrame:frame])
// Default colors.
self.backgroundColor = [UIColor clearColor];
self.dotColorCurrentPage = [UIColor blackColor];
self.dotColorOtherPage = [UIColor lightGrayColor];
return self;
-(id)initWithCoder:(NSCoder *)aDecoder
if (self = [super initWithCoder:aDecoder])
self.dotColorCurrentPage = [UIColor blackColor];
self.dotColorOtherPage = [UIColor lightGrayColor];
return self;
- (void)drawRect:(CGRect)rect
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetAllowsAntialiasing(context, true);
CGRect currentBounds = self.bounds;
CGFloat dotsWidth = self.numberOfPages*kDotDiameter + MAX(0, self.numberOfPages-1)*kDotSpacer;
CGFloat x = CGRectGetMidX(currentBounds)-dotsWidth/2;
CGFloat y = CGRectGetMidY(currentBounds)-kDotDiameter/2;
for (int i=0; i<self.numberOfPages; i++)
CGRect circleRect = CGRectMake(x, y, kDotDiameter, kDotDiameter);
if (i == self.currentPage)
CGContextSetFillColorWithColor(context, self.dotColorCurrentPage.CGColor);
else
CGContextSetFillColorWithColor(context, self.dotColorOtherPage.CGColor);
CGContextFillEllipseInRect(context, circleRect);
x += kDotDiameter + kDotSpacer;
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
(x>dotSpanX)
@end
In case anyone wants an ARC / modern version of it (no need to redefine properties as ivar, no dealloc, and works with Interface Builder) :
#import <UIKit/UIKit.h>
@protocol PageControlDelegate;
@interface PageControl : UIView
// Set these to control the PageControl.
@property (nonatomic) NSInteger currentPage;
@property (nonatomic) NSInteger numberOfPages;
// Customize these as well as the backgroundColor property.
@property (nonatomic, strong) UIColor *dotColorCurrentPage;
@property (nonatomic, strong) UIColor *dotColorOtherPage;
// Optional delegate for callbacks when user taps a page dot.
@property (nonatomic, weak) NSObject<PageControlDelegate> *delegate;
@end
@protocol PageControlDelegate<NSObject>
@optional
- (void)pageControlPageDidChange:(PageControl *)pageControl;
@end
PageControl.m :
#import "PageControl.h"
// Tweak these or make them dynamic.
#define kDotDiameter 7.0
#define kDotSpacer 7.0
@implementation PageControl
@synthesize dotColorCurrentPage;
@synthesize dotColorOtherPage;
@synthesize currentPage;
@synthesize numberOfPages;
@synthesize delegate;
- (void)setCurrentPage:(NSInteger)page
currentPage = MIN(MAX(0, page), self.numberOfPages-1);
[self setNeedsDisplay];
- (void)setNumberOfPages:(NSInteger)pages
numberOfPages = MAX(0, pages);
currentPage = MIN(MAX(0, self.currentPage), numberOfPages-1);
[self setNeedsDisplay];
- (id)initWithFrame:(CGRect)frame
if (self = [super initWithFrame:frame])
// Default colors.
self.backgroundColor = [UIColor clearColor];
self.dotColorCurrentPage = [UIColor blackColor];
self.dotColorOtherPage = [UIColor lightGrayColor];
return self;
-(id)initWithCoder:(NSCoder *)aDecoder
if (self = [super initWithCoder:aDecoder])
self.dotColorCurrentPage = [UIColor blackColor];
self.dotColorOtherPage = [UIColor lightGrayColor];
return self;
- (void)drawRect:(CGRect)rect
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetAllowsAntialiasing(context, true);
CGRect currentBounds = self.bounds;
CGFloat dotsWidth = self.numberOfPages*kDotDiameter + MAX(0, self.numberOfPages-1)*kDotSpacer;
CGFloat x = CGRectGetMidX(currentBounds)-dotsWidth/2;
CGFloat y = CGRectGetMidY(currentBounds)-kDotDiameter/2;
for (int i=0; i<self.numberOfPages; i++)
CGRect circleRect = CGRectMake(x, y, kDotDiameter, kDotDiameter);
if (i == self.currentPage)
CGContextSetFillColorWithColor(context, self.dotColorCurrentPage.CGColor);
else
CGContextSetFillColorWithColor(context, self.dotColorOtherPage.CGColor);
CGContextFillEllipseInRect(context, circleRect);
x += kDotDiameter + kDotSpacer;
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
(x>dotSpanX)
@end
answered Jun 14 '12 at 21:59
Ben GBen G
3,59232630
3,59232630
1
A minor addition to stop it sending to the delegate if the page number didn't actually change after a touch. NSInteger newPage = floor(x/(kDotDiameter+kDotSpacer)); if (self.currentPage == newPage) return;
– theLastNightTrain
Jul 16 '12 at 12:59
add a comment |
1
A minor addition to stop it sending to the delegate if the page number didn't actually change after a touch. NSInteger newPage = floor(x/(kDotDiameter+kDotSpacer)); if (self.currentPage == newPage) return;
– theLastNightTrain
Jul 16 '12 at 12:59
1
1
A minor addition to stop it sending to the delegate if the page number didn't actually change after a touch. NSInteger newPage = floor(x/(kDotDiameter+kDotSpacer)); if (self.currentPage == newPage) return;
– theLastNightTrain
Jul 16 '12 at 12:59
A minor addition to stop it sending to the delegate if the page number didn't actually change after a touch. NSInteger newPage = floor(x/(kDotDiameter+kDotSpacer)); if (self.currentPage == newPage) return;
– theLastNightTrain
Jul 16 '12 at 12:59
add a comment |
The answer provided by Heiberg works really well, however the page control does not behave exactly like the one by apple.
If you want the page control to behave like the one from apple does (always increment the current page by one if you touch the second half, otherwise decrease by one), try this touchesBegan-method instead:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
CGPoint touchPoint = [[[event touchesForView:self] anyObject] locationInView:self];
CGRect currentBounds = self.bounds;
CGFloat x = touchPoint.x - CGRectGetMidX(currentBounds);
if(x<0 && self.currentPage>=0)
self.currentPage--;
[self.delegate pageControlPageDidChange:self];
else if(x>0 && self.currentPage<self.numberOfPages-1)
self.currentPage++;
[self.delegate pageControlPageDidChange:self];
add a comment |
The answer provided by Heiberg works really well, however the page control does not behave exactly like the one by apple.
If you want the page control to behave like the one from apple does (always increment the current page by one if you touch the second half, otherwise decrease by one), try this touchesBegan-method instead:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
CGPoint touchPoint = [[[event touchesForView:self] anyObject] locationInView:self];
CGRect currentBounds = self.bounds;
CGFloat x = touchPoint.x - CGRectGetMidX(currentBounds);
if(x<0 && self.currentPage>=0)
self.currentPage--;
[self.delegate pageControlPageDidChange:self];
else if(x>0 && self.currentPage<self.numberOfPages-1)
self.currentPage++;
[self.delegate pageControlPageDidChange:self];
add a comment |
The answer provided by Heiberg works really well, however the page control does not behave exactly like the one by apple.
If you want the page control to behave like the one from apple does (always increment the current page by one if you touch the second half, otherwise decrease by one), try this touchesBegan-method instead:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
CGPoint touchPoint = [[[event touchesForView:self] anyObject] locationInView:self];
CGRect currentBounds = self.bounds;
CGFloat x = touchPoint.x - CGRectGetMidX(currentBounds);
if(x<0 && self.currentPage>=0)
self.currentPage--;
[self.delegate pageControlPageDidChange:self];
else if(x>0 && self.currentPage<self.numberOfPages-1)
self.currentPage++;
[self.delegate pageControlPageDidChange:self];
The answer provided by Heiberg works really well, however the page control does not behave exactly like the one by apple.
If you want the page control to behave like the one from apple does (always increment the current page by one if you touch the second half, otherwise decrease by one), try this touchesBegan-method instead:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
CGPoint touchPoint = [[[event touchesForView:self] anyObject] locationInView:self];
CGRect currentBounds = self.bounds;
CGFloat x = touchPoint.x - CGRectGetMidX(currentBounds);
if(x<0 && self.currentPage>=0)
self.currentPage--;
[self.delegate pageControlPageDidChange:self];
else if(x>0 && self.currentPage<self.numberOfPages-1)
self.currentPage++;
[self.delegate pageControlPageDidChange:self];
edited Oct 24 '11 at 7:44
answered Oct 6 '11 at 16:08
ChristophKChristophK
2,47721924
2,47721924
add a comment |
add a comment |
Add the following code to DidFinishLauch in AppDelegate,
UIPageControl *pageControl = [UIPageControl appearance];
pageControl.pageIndicatorTintColor = [UIColor lightGrayColor];
pageControl.currentPageIndicatorTintColor = [UIColor blackColor];
pageControl.backgroundColor = [UIColor whiteColor];
Hope this will help.
add a comment |
Add the following code to DidFinishLauch in AppDelegate,
UIPageControl *pageControl = [UIPageControl appearance];
pageControl.pageIndicatorTintColor = [UIColor lightGrayColor];
pageControl.currentPageIndicatorTintColor = [UIColor blackColor];
pageControl.backgroundColor = [UIColor whiteColor];
Hope this will help.
add a comment |
Add the following code to DidFinishLauch in AppDelegate,
UIPageControl *pageControl = [UIPageControl appearance];
pageControl.pageIndicatorTintColor = [UIColor lightGrayColor];
pageControl.currentPageIndicatorTintColor = [UIColor blackColor];
pageControl.backgroundColor = [UIColor whiteColor];
Hope this will help.
Add the following code to DidFinishLauch in AppDelegate,
UIPageControl *pageControl = [UIPageControl appearance];
pageControl.pageIndicatorTintColor = [UIColor lightGrayColor];
pageControl.currentPageIndicatorTintColor = [UIColor blackColor];
pageControl.backgroundColor = [UIColor whiteColor];
Hope this will help.
answered Feb 3 '14 at 6:39
poshaposha
51821125
51821125
add a comment |
add a comment |
use this for coding
if ([pageControl respondsToSelector:@selector(setPageIndicatorTintColor:)])
pageControl.pageIndicatorTintColor = [UIColor whiteColor];
or from storyboard you can change from current page tint
Thanks... keep sharing :)
– Tirth
Sep 7 '15 at 6:01
add a comment |
use this for coding
if ([pageControl respondsToSelector:@selector(setPageIndicatorTintColor:)])
pageControl.pageIndicatorTintColor = [UIColor whiteColor];
or from storyboard you can change from current page tint
Thanks... keep sharing :)
– Tirth
Sep 7 '15 at 6:01
add a comment |
use this for coding
if ([pageControl respondsToSelector:@selector(setPageIndicatorTintColor:)])
pageControl.pageIndicatorTintColor = [UIColor whiteColor];
or from storyboard you can change from current page tint
use this for coding
if ([pageControl respondsToSelector:@selector(setPageIndicatorTintColor:)])
pageControl.pageIndicatorTintColor = [UIColor whiteColor];
or from storyboard you can change from current page tint
answered Sep 2 '15 at 9:53
Pooja PatelPooja Patel
578710
578710
Thanks... keep sharing :)
– Tirth
Sep 7 '15 at 6:01
add a comment |
Thanks... keep sharing :)
– Tirth
Sep 7 '15 at 6:01
Thanks... keep sharing :)
– Tirth
Sep 7 '15 at 6:01
Thanks... keep sharing :)
– Tirth
Sep 7 '15 at 6:01
add a comment |
Adding to existing answers, it can be done like,
add a comment |
Adding to existing answers, it can be done like,
add a comment |
Adding to existing answers, it can be done like,
Adding to existing answers, it can be done like,
answered Aug 7 '14 at 6:44
aTozaToz
2,21611933
2,21611933
add a comment |
add a comment |
In Swift, this code inside the UIPageViewController is getting a reference to the page indicator and setting its properties
override func viewDidLoad()
super.viewDidLoad()
//Creating the proxy
let pageControl = UIPageControl.appearance()
//Customizing
pageControl.pageIndicatorTintColor = UIColor.lightGrayColor()
pageControl.currentPageIndicatorTintColor = UIColor.darkGrayColor()
//Setting the background of the view controller so the dots wont be on a black background
self.view.backgroundColor = UIColor.whiteColor()
UIPageControl
is not the same asUIPageViewController
– jungledev
Jul 12 '18 at 14:02
add a comment |
In Swift, this code inside the UIPageViewController is getting a reference to the page indicator and setting its properties
override func viewDidLoad()
super.viewDidLoad()
//Creating the proxy
let pageControl = UIPageControl.appearance()
//Customizing
pageControl.pageIndicatorTintColor = UIColor.lightGrayColor()
pageControl.currentPageIndicatorTintColor = UIColor.darkGrayColor()
//Setting the background of the view controller so the dots wont be on a black background
self.view.backgroundColor = UIColor.whiteColor()
UIPageControl
is not the same asUIPageViewController
– jungledev
Jul 12 '18 at 14:02
add a comment |
In Swift, this code inside the UIPageViewController is getting a reference to the page indicator and setting its properties
override func viewDidLoad()
super.viewDidLoad()
//Creating the proxy
let pageControl = UIPageControl.appearance()
//Customizing
pageControl.pageIndicatorTintColor = UIColor.lightGrayColor()
pageControl.currentPageIndicatorTintColor = UIColor.darkGrayColor()
//Setting the background of the view controller so the dots wont be on a black background
self.view.backgroundColor = UIColor.whiteColor()
In Swift, this code inside the UIPageViewController is getting a reference to the page indicator and setting its properties
override func viewDidLoad()
super.viewDidLoad()
//Creating the proxy
let pageControl = UIPageControl.appearance()
//Customizing
pageControl.pageIndicatorTintColor = UIColor.lightGrayColor()
pageControl.currentPageIndicatorTintColor = UIColor.darkGrayColor()
//Setting the background of the view controller so the dots wont be on a black background
self.view.backgroundColor = UIColor.whiteColor()
edited Oct 4 '17 at 3:53
Pang
6,9891666105
6,9891666105
answered Jul 22 '16 at 17:13
Arbel IsraeliArbel Israeli
7322819
7322819
UIPageControl
is not the same asUIPageViewController
– jungledev
Jul 12 '18 at 14:02
add a comment |
UIPageControl
is not the same asUIPageViewController
– jungledev
Jul 12 '18 at 14:02
UIPageControl
is not the same as UIPageViewController
– jungledev
Jul 12 '18 at 14:02
UIPageControl
is not the same as UIPageViewController
– jungledev
Jul 12 '18 at 14:02
add a comment |
It's easy with Swift 1.2:
UIPageControl.appearance().pageIndicatorTintColor = UIColor.lightGrayColor()
UIPageControl.appearance().currentPageIndicatorTintColor = UIColor.redColor()
2
This sets it globally. If you have multiple UIPageControls in your app and you need different colors based on class then useUIPageControl.appearanceWhenContainedInInstancesOfClasses([MyClassName.self])
instead ofUIPageControl.appearance()
. Requires iOS 9.
– Jon
Jan 29 '16 at 17:10
add a comment |
It's easy with Swift 1.2:
UIPageControl.appearance().pageIndicatorTintColor = UIColor.lightGrayColor()
UIPageControl.appearance().currentPageIndicatorTintColor = UIColor.redColor()
2
This sets it globally. If you have multiple UIPageControls in your app and you need different colors based on class then useUIPageControl.appearanceWhenContainedInInstancesOfClasses([MyClassName.self])
instead ofUIPageControl.appearance()
. Requires iOS 9.
– Jon
Jan 29 '16 at 17:10
add a comment |
It's easy with Swift 1.2:
UIPageControl.appearance().pageIndicatorTintColor = UIColor.lightGrayColor()
UIPageControl.appearance().currentPageIndicatorTintColor = UIColor.redColor()
It's easy with Swift 1.2:
UIPageControl.appearance().pageIndicatorTintColor = UIColor.lightGrayColor()
UIPageControl.appearance().currentPageIndicatorTintColor = UIColor.redColor()
edited Sep 5 '15 at 15:34
josliber♦
37.5k1165104
37.5k1165104
answered Sep 5 '15 at 7:13
Oleg PopovOleg Popov
1,47411214
1,47411214
2
This sets it globally. If you have multiple UIPageControls in your app and you need different colors based on class then useUIPageControl.appearanceWhenContainedInInstancesOfClasses([MyClassName.self])
instead ofUIPageControl.appearance()
. Requires iOS 9.
– Jon
Jan 29 '16 at 17:10
add a comment |
2
This sets it globally. If you have multiple UIPageControls in your app and you need different colors based on class then useUIPageControl.appearanceWhenContainedInInstancesOfClasses([MyClassName.self])
instead ofUIPageControl.appearance()
. Requires iOS 9.
– Jon
Jan 29 '16 at 17:10
2
2
This sets it globally. If you have multiple UIPageControls in your app and you need different colors based on class then use
UIPageControl.appearanceWhenContainedInInstancesOfClasses([MyClassName.self])
instead of UIPageControl.appearance()
. Requires iOS 9.– Jon
Jan 29 '16 at 17:10
This sets it globally. If you have multiple UIPageControls in your app and you need different colors based on class then use
UIPageControl.appearanceWhenContainedInInstancesOfClasses([MyClassName.self])
instead of UIPageControl.appearance()
. Requires iOS 9.– Jon
Jan 29 '16 at 17:10
add a comment |
You can fix it with ease by adding the following code to your appdelegate.m file in your didFinishLaunchingWithOptions
method:
UIPageControl *pageControl = [UIPageControl appearance];
pageControl.pageIndicatorTintColor = [UIColor darkGrayColor];
pageControl.currentPageIndicatorTintColor = [UIColor orangeColor];
pageControl.backgroundColor = [UIColor whiteColor]
add a comment |
You can fix it with ease by adding the following code to your appdelegate.m file in your didFinishLaunchingWithOptions
method:
UIPageControl *pageControl = [UIPageControl appearance];
pageControl.pageIndicatorTintColor = [UIColor darkGrayColor];
pageControl.currentPageIndicatorTintColor = [UIColor orangeColor];
pageControl.backgroundColor = [UIColor whiteColor]
add a comment |
You can fix it with ease by adding the following code to your appdelegate.m file in your didFinishLaunchingWithOptions
method:
UIPageControl *pageControl = [UIPageControl appearance];
pageControl.pageIndicatorTintColor = [UIColor darkGrayColor];
pageControl.currentPageIndicatorTintColor = [UIColor orangeColor];
pageControl.backgroundColor = [UIColor whiteColor]
You can fix it with ease by adding the following code to your appdelegate.m file in your didFinishLaunchingWithOptions
method:
UIPageControl *pageControl = [UIPageControl appearance];
pageControl.pageIndicatorTintColor = [UIColor darkGrayColor];
pageControl.currentPageIndicatorTintColor = [UIColor orangeColor];
pageControl.backgroundColor = [UIColor whiteColor]
edited Mar 18 '18 at 8:44
honk
4,947114450
4,947114450
answered Feb 25 '15 at 14:29
Nabil El AtlasNabil El Atlas
463
463
add a comment |
add a comment |
This is worked for me in iOS 7.
pageControl.pageIndicatorTintColor = [UIColor purpleColor];
pageControl.currentPageIndicatorTintColor = [UIColor magentaColor];
add a comment |
This is worked for me in iOS 7.
pageControl.pageIndicatorTintColor = [UIColor purpleColor];
pageControl.currentPageIndicatorTintColor = [UIColor magentaColor];
add a comment |
This is worked for me in iOS 7.
pageControl.pageIndicatorTintColor = [UIColor purpleColor];
pageControl.currentPageIndicatorTintColor = [UIColor magentaColor];
This is worked for me in iOS 7.
pageControl.pageIndicatorTintColor = [UIColor purpleColor];
pageControl.currentPageIndicatorTintColor = [UIColor magentaColor];
answered Feb 3 '14 at 6:34
SidSid
12711
12711
add a comment |
add a comment |
It's not possible using the iPhone SDK from an official standpoint. You might be able to do it using private methods, but that will be a barrier to getting onto the app store.
The only other safe solution is to create yout own page control which shpuldnt be too difficult given that the page control simply displays what page is currently shown in a scroll view.
Theere isn't a link to my solution. My solution is there in text just above your comment. Either look for the private methods (I won't know what these are) or write your own (I'm not going to do that for you).
– Jasarien
May 31 '10 at 13:41
add a comment |
It's not possible using the iPhone SDK from an official standpoint. You might be able to do it using private methods, but that will be a barrier to getting onto the app store.
The only other safe solution is to create yout own page control which shpuldnt be too difficult given that the page control simply displays what page is currently shown in a scroll view.
Theere isn't a link to my solution. My solution is there in text just above your comment. Either look for the private methods (I won't know what these are) or write your own (I'm not going to do that for you).
– Jasarien
May 31 '10 at 13:41
add a comment |
It's not possible using the iPhone SDK from an official standpoint. You might be able to do it using private methods, but that will be a barrier to getting onto the app store.
The only other safe solution is to create yout own page control which shpuldnt be too difficult given that the page control simply displays what page is currently shown in a scroll view.
It's not possible using the iPhone SDK from an official standpoint. You might be able to do it using private methods, but that will be a barrier to getting onto the app store.
The only other safe solution is to create yout own page control which shpuldnt be too difficult given that the page control simply displays what page is currently shown in a scroll view.
answered May 31 '10 at 12:44
JasarienJasarien
47.1k25144182
47.1k25144182
Theere isn't a link to my solution. My solution is there in text just above your comment. Either look for the private methods (I won't know what these are) or write your own (I'm not going to do that for you).
– Jasarien
May 31 '10 at 13:41
add a comment |
Theere isn't a link to my solution. My solution is there in text just above your comment. Either look for the private methods (I won't know what these are) or write your own (I'm not going to do that for you).
– Jasarien
May 31 '10 at 13:41
Theere isn't a link to my solution. My solution is there in text just above your comment. Either look for the private methods (I won't know what these are) or write your own (I'm not going to do that for you).
– Jasarien
May 31 '10 at 13:41
Theere isn't a link to my solution. My solution is there in text just above your comment. Either look for the private methods (I won't know what these are) or write your own (I'm not going to do that for you).
– Jasarien
May 31 '10 at 13:41
add a comment |
@Jasarien I think you can subclass UIPageControll, line picked from apple doc only "Subclasses that customize the appearance of the page control can use this method to resize the page control when the page count changes" for the method sizeForNumberOfPages:
add a comment |
@Jasarien I think you can subclass UIPageControll, line picked from apple doc only "Subclasses that customize the appearance of the page control can use this method to resize the page control when the page count changes" for the method sizeForNumberOfPages:
add a comment |
@Jasarien I think you can subclass UIPageControll, line picked from apple doc only "Subclasses that customize the appearance of the page control can use this method to resize the page control when the page count changes" for the method sizeForNumberOfPages:
@Jasarien I think you can subclass UIPageControll, line picked from apple doc only "Subclasses that customize the appearance of the page control can use this method to resize the page control when the page count changes" for the method sizeForNumberOfPages:
answered Aug 1 '11 at 10:48
dsawdsaw
552512
552512
add a comment |
add a comment |
You could also use Three20 Library that contains a styleable PageControl and dozens of other helpful UI Controls and Abstractions.
and the link is: three20.info
– Nir Pengas
Feb 13 '12 at 15:14
add a comment |
You could also use Three20 Library that contains a styleable PageControl and dozens of other helpful UI Controls and Abstractions.
and the link is: three20.info
– Nir Pengas
Feb 13 '12 at 15:14
add a comment |
You could also use Three20 Library that contains a styleable PageControl and dozens of other helpful UI Controls and Abstractions.
You could also use Three20 Library that contains a styleable PageControl and dozens of other helpful UI Controls and Abstractions.
answered Dec 6 '11 at 12:01
cschuffcschuff
3,99232448
3,99232448
and the link is: three20.info
– Nir Pengas
Feb 13 '12 at 15:14
add a comment |
and the link is: three20.info
– Nir Pengas
Feb 13 '12 at 15:14
and the link is: three20.info
– Nir Pengas
Feb 13 '12 at 15:14
and the link is: three20.info
– Nir Pengas
Feb 13 '12 at 15:14
add a comment |
In cased of Swift 2.0
and up, the below code will work:
pageControl.pageIndicatorTintColor = UIColor.whiteColor()
pageControl.currentPageIndicatorTintColor = UIColor.redColor()
add a comment |
In cased of Swift 2.0
and up, the below code will work:
pageControl.pageIndicatorTintColor = UIColor.whiteColor()
pageControl.currentPageIndicatorTintColor = UIColor.redColor()
add a comment |
In cased of Swift 2.0
and up, the below code will work:
pageControl.pageIndicatorTintColor = UIColor.whiteColor()
pageControl.currentPageIndicatorTintColor = UIColor.redColor()
In cased of Swift 2.0
and up, the below code will work:
pageControl.pageIndicatorTintColor = UIColor.whiteColor()
pageControl.currentPageIndicatorTintColor = UIColor.redColor()
answered Jan 12 '16 at 9:10
Sohil R. MemonSohil R. Memon
7,18512044
7,18512044
add a comment |
add a comment |
Here is a Swift 3.0 solution ... you know if you are OK with accepting stated risk: "Modifying the subviews of an existing control is fragile".
You will have to call updateDots() in your viewDidAppear() and your valueChanged handler for the page control.
import UIKit
class CustomImagePageControl: UIPageControl
let activeImage:UIImage = UIImage(named: "SelectedPage")!
let inactiveImage:UIImage = UIImage(named: "UnselectedPage")!
override func awakeFromNib()
super.awakeFromNib()
self.pageIndicatorTintColor = UIColor.clear
self.currentPageIndicatorTintColor = UIColor.clear
self.clipsToBounds = false
func updateDots()
var i = 0
for view in self.subviews
if let imageView = self.imageForSubview(view)
if i == self.currentPage
imageView.image = self.activeImage
else
imageView.image = self.inactiveImage
i = i + 1
else
var dotImage = self.inactiveImage
if i == self.currentPage
dotImage = self.activeImage
view.clipsToBounds = false
view.addSubview(UIImageView(image:dotImage))
i = i + 1
fileprivate func imageForSubview(_ view:UIView) -> UIImageView?
var dot:UIImageView?
if let dotImageView = view as? UIImageView
dot = dotImageView
else
for foundView in view.subviews
if let imageView = foundView as? UIImageView
dot = imageView
break
return dot
add a comment |
Here is a Swift 3.0 solution ... you know if you are OK with accepting stated risk: "Modifying the subviews of an existing control is fragile".
You will have to call updateDots() in your viewDidAppear() and your valueChanged handler for the page control.
import UIKit
class CustomImagePageControl: UIPageControl
let activeImage:UIImage = UIImage(named: "SelectedPage")!
let inactiveImage:UIImage = UIImage(named: "UnselectedPage")!
override func awakeFromNib()
super.awakeFromNib()
self.pageIndicatorTintColor = UIColor.clear
self.currentPageIndicatorTintColor = UIColor.clear
self.clipsToBounds = false
func updateDots()
var i = 0
for view in self.subviews
if let imageView = self.imageForSubview(view)
if i == self.currentPage
imageView.image = self.activeImage
else
imageView.image = self.inactiveImage
i = i + 1
else
var dotImage = self.inactiveImage
if i == self.currentPage
dotImage = self.activeImage
view.clipsToBounds = false
view.addSubview(UIImageView(image:dotImage))
i = i + 1
fileprivate func imageForSubview(_ view:UIView) -> UIImageView?
var dot:UIImageView?
if let dotImageView = view as? UIImageView
dot = dotImageView
else
for foundView in view.subviews
if let imageView = foundView as? UIImageView
dot = imageView
break
return dot
add a comment |
Here is a Swift 3.0 solution ... you know if you are OK with accepting stated risk: "Modifying the subviews of an existing control is fragile".
You will have to call updateDots() in your viewDidAppear() and your valueChanged handler for the page control.
import UIKit
class CustomImagePageControl: UIPageControl
let activeImage:UIImage = UIImage(named: "SelectedPage")!
let inactiveImage:UIImage = UIImage(named: "UnselectedPage")!
override func awakeFromNib()
super.awakeFromNib()
self.pageIndicatorTintColor = UIColor.clear
self.currentPageIndicatorTintColor = UIColor.clear
self.clipsToBounds = false
func updateDots()
var i = 0
for view in self.subviews
if let imageView = self.imageForSubview(view)
if i == self.currentPage
imageView.image = self.activeImage
else
imageView.image = self.inactiveImage
i = i + 1
else
var dotImage = self.inactiveImage
if i == self.currentPage
dotImage = self.activeImage
view.clipsToBounds = false
view.addSubview(UIImageView(image:dotImage))
i = i + 1
fileprivate func imageForSubview(_ view:UIView) -> UIImageView?
var dot:UIImageView?
if let dotImageView = view as? UIImageView
dot = dotImageView
else
for foundView in view.subviews
if let imageView = foundView as? UIImageView
dot = imageView
break
return dot
Here is a Swift 3.0 solution ... you know if you are OK with accepting stated risk: "Modifying the subviews of an existing control is fragile".
You will have to call updateDots() in your viewDidAppear() and your valueChanged handler for the page control.
import UIKit
class CustomImagePageControl: UIPageControl
let activeImage:UIImage = UIImage(named: "SelectedPage")!
let inactiveImage:UIImage = UIImage(named: "UnselectedPage")!
override func awakeFromNib()
super.awakeFromNib()
self.pageIndicatorTintColor = UIColor.clear
self.currentPageIndicatorTintColor = UIColor.clear
self.clipsToBounds = false
func updateDots()
var i = 0
for view in self.subviews
if let imageView = self.imageForSubview(view)
if i == self.currentPage
imageView.image = self.activeImage
else
imageView.image = self.inactiveImage
i = i + 1
else
var dotImage = self.inactiveImage
if i == self.currentPage
dotImage = self.activeImage
view.clipsToBounds = false
view.addSubview(UIImageView(image:dotImage))
i = i + 1
fileprivate func imageForSubview(_ view:UIView) -> UIImageView?
var dot:UIImageView?
if let dotImageView = view as? UIImageView
dot = dotImageView
else
for foundView in view.subviews
if let imageView = foundView as? UIImageView
dot = imageView
break
return dot
answered Dec 2 '16 at 18:25
CodenameDuchessCodenameDuchess
8461219
8461219
add a comment |
add a comment |
myView.superview.tintColor = [UIColor colorWithRed:1.0f
green:1.0f blue:1.0f alpha:1.0f];
add a comment |
myView.superview.tintColor = [UIColor colorWithRed:1.0f
green:1.0f blue:1.0f alpha:1.0f];
add a comment |
myView.superview.tintColor = [UIColor colorWithRed:1.0f
green:1.0f blue:1.0f alpha:1.0f];
myView.superview.tintColor = [UIColor colorWithRed:1.0f
green:1.0f blue:1.0f alpha:1.0f];
edited Sep 28 '13 at 16:05
kleopatra
45k1676163
45k1676163
answered Sep 28 '13 at 16:01
Michael BelangerMichael Belanger
1
1
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f2942636%2fhow-can-i-change-the-color-of-pagination-dots-of-uipagecontrol%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown