Created
July 31, 2013 12:00
-
-
Save a2ikm/6121382 to your computer and use it in GitHub Desktop.
Customize UIPageControl's image
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <UIKit/UIKit.h> | |
@interface MyPageControl : UIPageControl | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "MyPageControl.h" | |
@implementation MyPageControl | |
- (id)initWithFrame:(CGRect)frame | |
{ | |
self = [super initWithFrame:frame]; | |
if (self) { | |
self.backgroundColor = [UIColor clearColor]; | |
} | |
return self; | |
} | |
- (void)drawRect:(CGRect)rect | |
{ | |
for (NSInteger i = 0; i < self.numberOfPages; i++) { | |
UIImageView *pageIcon = [self.subviews objectAtIndex:i]; | |
if ([pageIcon isKindOfClass:[UIImageView class]]) { | |
if (i == self.currentPage) { | |
pageIcon.image = [UIImage imageNamed:@"page_on.png"]; | |
} else { | |
pageIcon.image = [UIImage imageNamed:@"page_off.png"]; | |
} | |
} | |
} | |
} | |
- (void)setCurrentPage:(NSInteger)curPageIndex | |
{ | |
[super setCurrentPage:curPageIndex]; | |
[self setNeedsDisplay]; | |
} | |
@end |
pageControl.userInteractionEnabled = YES
だとタップしたときにデフォルトの画像で描画されてしまう。デフォルトはYES
。
とはいえinit
のなかで設定するのも微妙だし、適宜書き換えが必要。
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
http://yusks.wordpress.com/2010/11/11/uipagecontrol%E3%81%AE%E3%82%A2%E3%82%A4%E3%82%B3%E3%83%B3%E3%82%92%E5%A4%89%E3%81%88%E3%82%8B/ を参考に若干修正。