UIPageControl using Auto Layout (UIScrollView issue)

This content has 12 years. Please, read this page keeping its age in your mind.

As your app design and development goes on you decide that is good to use a UIPageControl to navigate though a series of UIView (or UIViewControllers).

If you are using iOS 6, Auto Layout is enabled on your storyboard. Later you will discover that this is the point of trouble.

If you google around to find a suitable example probably you will find:

Using UIPageControl as a container UIViewController from wannabegeek blog

a great tutorial that works in iOS 5.

In iOS 6, auto layout refuse to work with UIScrollView as it is in this tutorial.

If you look around (iOS 6 release notes), you will find that obviously there are some changes.

So what I can to do in order to have my UIPageControl working?

In PageViewController.m, modify viewWillAppear as follow:

- (void)viewWillAppear:(BOOL)animated {

     [super viewWillAppear:animated];

     // add this in order UIScrollView work with Auto layout

     if (IS_WIDESCREEN) {

          self.scrollView.frame = CGRectMake(0, 0, 320, 468);

      } else {

          self.scrollView.frame = CGRectMake(0, 0, 320, 380);

     }

...

}

and in the same fashion viewWillDisappear

- (void)viewWillDisappear:(BOOL)animated {

....

// add this in order UIScrollView work with Auto layout

      if (IS_WIDESCREEN) {

           self.scrollView.contentSize = CGSizeMake(320, 468);

      } else {

           self.scrollView.contentSize = CGSizeMake(320, 380);

      }

}

Credits to Mr Kadore
IS_WIDESCREEN is the following macro

#define IS_WIDESCREEN ( fabs( ( double )[ [ UIScreen mainScreen ] bounds ].size.height - ( double )568 ) < DBL_EPSILON )

in order to recognise iOS devices with screen height of 568, like iPhone 5.

3 thoughts on “UIPageControl using Auto Layout (UIScrollView issue)

  1. Works perfect! Thank you. I have been trying to figure out why all my scroll views died after enabling auto layout. Perfect fix

  2. Hi, something weird is happening: It’s not loading properly the view controller size and it crashes!

Comments are closed.