Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>UPDATE:</strong></p> <p>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 <code>pageIndicatorTintColor</code> and <code>currentPageIndicatorTintColor</code> properties on <code>UIPageControl</code>.</p> <p><strong>ORIGINAL ANSWER:</strong></p> <p>I ran into this problem today and decided to write my own simple replacement class. </p> <p>It's a sublassed UIView that uses Core Graphics to render the dots in the colors you specify.</p> <p>You use the exposed properties to customize and control it.</p> <p>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.</p> <p>It's completely fresh from the oven, but seems to work. Let me know if you run into any problems with it.</p> <p>Future improvements: </p> <ul> <li>Resize the dots to fit the current bounds if there are too many. </li> <li>Don't redraw the entire view in drawRect:</li> </ul> <p>Example use:</p> <pre><code>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]; </code></pre> <p>Header file:</p> <pre><code>// // PageControl.h // // Replacement for UIPageControl because that one only supports white dots. // // Created by Morten Heiberg &lt;morten@heiberg.net&gt; on November 1, 2010. // #import &lt;UIKit/UIKit.h&gt; @protocol PageControlDelegate; @interface PageControl : UIView { @private NSInteger _currentPage; NSInteger _numberOfPages; UIColor *dotColorCurrentPage; UIColor *dotColorOtherPage; NSObject&lt;PageControlDelegate&gt; *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&lt;PageControlDelegate&gt; *delegate; @end @protocol PageControlDelegate&lt;NSObject&gt; @optional - (void)pageControlPageDidChange:(PageControl *)pageControl; @end </code></pre> <p>Implementation file:</p> <pre><code>// // PageControl.m // // Replacement for UIPageControl because that one only supports white dots. // // Created by Morten Heiberg &lt;morten@heiberg.net&gt; 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&lt;_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 { if (!self.delegate) return; CGPoint touchPoint = [[[event touchesForView:self] anyObject] locationInView:self]; CGFloat dotSpanX = self.numberOfPages*(kDotDiameter + kDotSpacer); CGFloat dotSpanY = kDotDiameter + kDotSpacer; CGRect currentBounds = self.bounds; CGFloat x = touchPoint.x + dotSpanX/2 - CGRectGetMidX(currentBounds); CGFloat y = touchPoint.y + dotSpanY/2 - CGRectGetMidY(currentBounds); if ((x&lt;0) || (x&gt;dotSpanX) || (y&lt;0) || (y&gt;dotSpanY)) return; self.currentPage = floor(x/(kDotDiameter+kDotSpacer)); if ([self.delegate respondsToSelector:@selector(pageControlPageDidChange:)]) { [self.delegate pageControlPageDidChange:self]; } } @end </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload