Search code examples
iphoneios6uiscrollviewuipagecontrol

UIScrollView with page control


Have three different page view controllers using as views in uiscrollview. UIScrollView with pagecontrol is displaying only third page as current page but not displaying two other pages. Pagecontrol is also not visible.

- (void)viewDidLoad
{
[super viewDidLoad];
PageOne *pageOne = [[PageOne alloc] init];

pageOne.view.frame = CGRectMake(0, 0, 320, 420);

PageTwo *pageTwo = [[PageTwo alloc]init];

pageTwo.view.frame = CGRectMake(0, 0, 320, 420);

PageThree *pageThree = [[PageThree alloc] init];

pageThree.view.frame = CGRectMake(0, 0, 320, 420);

scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)]; 

[scrollView setContentSize:CGSizeMake(self.view.frame.size.width * 3, self.view.frame.size.height)]; 

[scrollView setDelegate:self]; 

[scrollView setPagingEnabled:YES]; 

[scrollView setShowsHorizontalScrollIndicator:YES]; 

[scrollView addSubview:pageOne.view];

[scrollView addSubview:pageTwo.view];

[scrollView addSubview:pageThree.view];

[self.view addSubview:scrollView];

_pageControl = [[UIPageControl alloc] init];

[_pageControl setCurrentPage:0]; 

[_pageControl setNumberOfPages:3]; 

[_pageControl sizeToFit]; 

[_pageControl setFrame:CGRectMake((scrollView.frame.size.width / 2) - (_pageControl.frame.size.width / 2), scrollView.frame.size.height - _pageControl.frame.size.height, _pageControl.frame.size.width, _pageControl.frame.size.height)]; 

[self.view addSubview:self.pageControl]; 


[super viewDidLoad];
}

 - (void)scrollViewDidScroll:(UIScrollView *)scrollView {

_pageControl.currentPage = lround(scrollView.contentOffset.x / scrollView.frame.size.width); 

}

Right now it is displaying only pageThree. Why not displaying pageOne and pageTwo.

Why not displaying PageOne, pageTwo and then pageThree in series.

Thanks for help.


Solution

  • My first answer addressed the wrong thing...You can only see page 3 because all three pages are added on top of each other. Try:

    page1 CGRectMake(0,0,320,420) page2 CGRectMake(320,0,320,420) page3 CGRectMake(640,0,320,420)

    then they will be in the scroll view next to each other instead of on top of each other.