How to programmatically set the current page for UIPageControl?

20,814

(From the PageControl example)

To go directly to a particular section of the scrollView use

CGRect frame = scrollView.frame;
frame.origin.x = frame.size.width * page;
frame.origin.y = 0;
[scrollView scrollRectToVisible:frame animated:YES];

I just implemented the same, e.g scrollview and page control and used the

pageControl.currentPage = {pageNumber};

which worked perfectly, are you sure that the pageControl outlet is set correctly in Interface Builder (if you're using it).

If the problem is that the scrollView is not working, and not the pageControl. Then make sure that you have the contentsize of the scrollView set correctly.

    scrollView.contentSize = CGSizeMake(320*numberOfPages, scrollView.frame.size.height);

To this scrollview you then add the content to each section

page1ViewController.view.frame = CGRectMake(0, 0, 320, 200);
[scrollView addSubview:page1ViewController.view];

Next page gets pushed over 1 screen width

page2ViewController.view.frame = CGRectMake(320, 0, 320, 200);
[scrollView addSubview:page2ViewController.view];
Share:
20,814
RexOnRoids
Author by

RexOnRoids

Just some kid.

Updated on May 03, 2020

Comments

  • RexOnRoids
    RexOnRoids about 4 years

    I have 3 "pages" (page 0, page 1, page 2) in a UIScrollView that are snapped to by finger swipes. There is a UIPageControl there too. The UIScrollView starts off presenting page 0. What I want to do is present page 3 FIRST sometimes. How can I do this programmatically.

    Simply setting currentPage (of UIPageControl) to the page number does nothing by the way.