iOS 8 UIPageViewController Applying Constraints After Transitions

AutolayoutIos8Xcode6Uipageviewcontroller

Autolayout Problem Overview


My app has a multi-pane tutorial-style view that users swipe through to learn about the app. This is implemented very much as described in this tutorial. Having implemented it for both iOS 7 and 8, I'm comparing how they work, and finding issues with the latter — I'm running Xcode 6 GM here.

It seems that the UIPageViewController is rendering the views after the transition is complete. I overrode the delegate methods to see what was going on:

- (void)pageViewController:(UIPageViewController *)pageViewController willTransitionToViewControllers:(NSArray *)pendingViewControllers
{
   NSLog(@"Frame size before: %@", NSStringFromCGRect([(UIViewController*)pendingViewControllers[0] view].frame));
}

- (void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray *)previousViewControllers transitionCompleted:(BOOL)completed
{
   NSLog(@"Frame size after: %@", NSStringFromCGRect([(UIViewController*)previousViewControllers[0] view].frame));
}

And here's a sample output:

Frame size before: {{0, 0}, {600, 600}}
Frame size after: {{0, 0}, {320, 568}}

This manifests like so: swipe to the left to pull in the next view, and note a 32-pt white space at the bottom of the new view. Once the transition is complete, it jerks into its proper layout.

Is this a bug in iOS 8, perhaps? I'm all out of guesses at this point.

Autolayout Solutions


Solution 1 - Autolayout

I figured out you need to base your constraints on the view and not the layout guide of your view controller. This will ensure your view controller respects the constraints you set prior to the transition in a PageViewController.

you can do this like so:

Constraints example

Remember to uncheck "Constrain to margins"

Solution 2 - Autolayout

For anyone having this issue, for me it appeared to be that I was laying out views "relative to margin" (a new feature in iOS 8).

Instead of:

enter image description here

Use:

enter image description here

Solution 3 - Autolayout

I've been struggling with this for a few days.

I tried to implement it by instantiating the page ViewController from the storyboard. There was a definite resizing occurring. Checking the frame size in

  1. pageViewController:viewControllerAfterViewController
  2. pageViewController:willTransitionToViewControllers
  3. pageViewController:didFinishAnimating:previousViewControllers

The frame size would always change between the calls to 1 and 3. Sometimes before 2 and sometimes after.

If you're also using storyboards, I was able to resolve the issue by extracting the page UI elements into its own XIB file, setting the constraints in IB and then creating the pages with a call to initWithNibName.

Not a complete answer but it returned me to feeling productive. Hope it helps.

Solution 4 - Autolayout

For me solution was to pin top of the tableView to superview: Editor -> Pin -> Top Space To SuperView (iOS8)

Solution 5 - Autolayout

It is a bug in iOS 8 but I found a workaround:

Add an empty view on top of the statusbar in your Storyboard. The height of the view should be as much as the y-value of your object that is being moved.

Image of correct constraints

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAaron VeghView Question on Stackoverflow
Solution 1 - AutolayoutDarryl BaylissView Answer on Stackoverflow
Solution 2 - AutolayoutdficklingView Answer on Stackoverflow
Solution 3 - AutolayoutwidanoView Answer on Stackoverflow
Solution 4 - AutolayoutEugene BraginetsView Answer on Stackoverflow
Solution 5 - AutolayoutAmiiQoView Answer on Stackoverflow