pageControl = [[UIPageControl alloc] init];
pageControl.frame = CGRectMake((self.view.bounds.size.width - 100) / 2.0,
self.view.frame.size.height - 40,
100,
20);
pageControl.numberOfPages = 3;
pageControl.pageIndicatorTintColor = [UIColor darkGrayColor];
pageControl.currentPageIndicatorTintColor = [UIColor cyanColor];
[self.view addSubview:pageControl];
- pageControl结合scrollView使用,scrollView必须打开分页,示例如下
#import "ViewController.h"
@interface ViewController ()<UIScrollViewDelegate>
{
UIScrollView *scrollView;
UIPageControl *pageControl;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
scrollView.bounces = NO;
scrollView.showsHorizontalScrollIndicator = NO;
scrollView.showsVerticalScrollIndicator = NO;
CGFloat foreX = self.view.frame.size.width;
for (int i = 1; i <= 3; i++) {
NSString *str = [NSString stringWithFormat:@"%d.jpg",i];
UIImage *image = [UIImage imageNamed:str];
CGSize size = image.size;
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.frame = CGRectMake(foreX * (i - 1), 20, size.width, self.view.frame.size.height - 20);
[scrollView addSubview:imageView];
}
scrollView.contentSize = CGSizeMake(3 * foreX, self.view.frame.size.height - 20);
[self.view addSubview:scrollView];
scrollView.pagingEnabled = YES;
scrollView.delegate = self;
pageControl = [[UIPageControl alloc] init];
pageControl.frame = CGRectMake((self.view.bounds.size.width - 100) / 2.0,
self.view.frame.size.height - 40,
100,
20);
pageControl.numberOfPages = 3;
pageControl.pageIndicatorTintColor = [UIColor darkGrayColor];
pageControl.currentPageIndicatorTintColor = [UIColor cyanColor];
[self.view addSubview:pageControl];
}
-(void)scrollViewDidScroll:(UIScrollView *)_scrollView{
CGPoint offset = _scrollView.contentOffset;
CGFloat w = self.view.frame.size.width;
pageControl.currentPage = (NSInteger)(offset.x / w);
}
@end
![Alt text]()