jer-k
jer-k

Reputation: 1423

Pushing ViewController with UIScrollView + UIPageControl onto specific page

I have a VC with a scrollview and pagecontrol implemented in it. It has three pages, which are all set up correctly and working fine. What I want to be able to do is have the VC load onto different pages depending on what button a user presses.

In the method the button calls I have

ViewController *vc = [[ViewController alloc] init];
vc.pageControl.currentPage = 2;
[self.navigationController pushViewController:vc animated:YES]; 

When the VC loads, it is loaded onto the correct page (as evidenced by the bubbles at the bottom of the page) but it is displaying the information from page[0]. If I start scrolling, it jumps back to page[0] no matter what I do.

My best guess is that I need to make the VC load to a specific x origin depending on what page I want to display, but I haven't been able to find anything related to this, so I'm quite lost.

Appreciate any help.

EDIT: Here is the code from the VC class. I didn't write this and it is my first time working with PageControl, so I'm trying to learn as I go.

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {

    self.scrollView = [[UIScrollView alloc] init];
    self.scrollView.pagingEnabled = TRUE;
    self.scrollView.showsVerticalScrollIndicator = NO;
    self.scrollView.showsHorizontalScrollIndicator = NO;
    self.scrollView.delegate = self;

    firstPage = [[UIView alloc] init];
    firstPage.tag = 0;

    secondPage = [[UIView alloc] init];
    secondPage.tag = 1;

    thirdPage = [[UIView alloc] init];
    thirdPage.tag = 2;

    pages = [[NSArray alloc] initWithObjects:firstPage, secondPage, thirdPage, nil];

    // Page Control
    self.pageControl = [[UIPageControl alloc] init];
    self.pageControl.numberOfPages = 3;
    self.pageControl.currentPage = 0;

    [self.view addSubview:self.scrollView];
    [self.view addSubview:self.pageControl];
}
return self;
}

- (void)viewDidLoad
{
[super viewDidLoad];

// First Page Setup
//
[self setupFirstPage];

// Second Page Setup
//
[self setupSecondPage];

// Third Page Setup
//
[self setupThirdPage];

// Frame everything
//
CGRect screenFrame = [[UIScreen mainScreen] applicationFrame];
CGRect navBarFrame = self.navigationController.navigationBar.frame;

self.scrollView.frame = CGRectMake(0, navBarFrame.size.height, 320, screenFrame.size.height - navBarFrame.size.height);
self.pageControl.frame = CGRectMake(0, screenFrame.size.height - 70, 320, 20);

firstPage.frame = CGRectMake(0, 0, self.scrollView.frame.size.width, self.scrollView.frame.size.height);
secondPage.frame = CGRectMake(320 * secondPage.tag, 0, self.scrollView.frame.size.width, self.scrollView.frame.size.height);
thirdPage.frame = CGRectMake(320 * thirdPage.tag, 0, self.scrollView.frame.size.width, self.scrollView.frame.size.height);

// Add Subviews
//
[self.scrollView addSubview:firstPage];
[self.scrollView addSubview:secondPage];
[self.scrollView addSubview:thirdPage];

self.scrollView.contentSize = CGSizeMake(320 * pages.count, self.scrollView.frame.size.height);
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
CGFloat pageWidth = self.scrollView.frame.size.width;
int page = floor((self.scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
self.pageControl.currentPage = page;
}

Upvotes: 1

Views: 1677

Answers (1)

Dima
Dima

Reputation: 23624

Try adding this function and calling it right after you set the page number:

- (void)pageSelectorChanged 
{
    CGRect frame = self.scrollView.frame;
    frame.origin.x = frame.size.width * self.pageControl.currentPage;
    frame.origin.y = 0;
    [self.scrollView scrollRectToVisible:frame animated:NO];
}

Upvotes: 2

Related Questions