How do I position a subview in UIPageViewController using Auto Layout?

autolayout, ios, objective-c, uipageviewcontroller, uiview

Solution

This seems to be happening because `UIPageViewController`'s view (which is actually a `_UIPageViewControllerContentView`) doesn't handle subviews and autolayout as expected (at least in iOS 7; it worked fine for me in iOS 8).

You can get around this by not using autolayout, but I wanted to use autolayout so I ended up redoing my view controllers to avoid adding subviews to `UIPageViewController`.

Instead of subclassing `UIPageViewController`, I created a container view controller (a subclass of `UIViewController`) with a `UIPageViewController` as a child view controller. The custom subviews that I initially had in `UIPageViewController` were then added to the container's `view`. I also made the container the data source and delegate for the `UIPageViewController`.

There's a related thread where people got the same assertion failure but with a `UITableViewCell`: "Auto Layout still required after executing -layoutSubviews" with UITableViewCell subclass. Most of the suggestions there don't work or aren't applicable to UIPageViewController, but it helped me figure out why adding subviews with autolayout might cause a problem.

Problem

I have a `UIPageViewController` subclass, and in its `viewDidLoad` I want to add a `UILabel` to its `self.view`. This works fine if I set its position using frames, but if I try to position it using Auto Layout, I get: * Assertion failure in -[_UIPageViewControllerContentView layoutSublayersOfLayer:], /SourceCache/UIKit_Sim/UIKit-2935.58/UIView.m:8742 How am I supposed to position this UILabel to show up in the `UIPageViewController`?

Original source

Related problems