So I've been wracking my brains for a couple of days now trying to figure out this problem. Context: I have a custom UIPageViewController which I'm adding as a child to another view controller. This works perfectly fine in iOS 8, but crashes with the following error in iOS 7.1:
*** Assertion failure in -[_UIPageViewControllerContentView layoutSublayersOfLayer:], /SourceCache/UIKit_Sim/UIKit-2935.137/UIView.m:8794
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Auto Layout still required after executing -layoutSubviews. _UIPageViewControllerContentView's implementation of -layoutSubviews needs to call super.'
This is a similar problem to the one posted here and in a few other places, but none of the solutions have worked for me yet (and I have tried many).
Now, what is interesting is that if I don't add any subviews or any page view controllers, it runs. But if I write this in viewDidLoad, it's enough to make it crash with the same error as above:
UIView *myView = [[UIView alloc] init];
myView.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:myView];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[myView]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(myView)]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[myView(60)]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(myView)]];
Or if I add page view controllers, it crashes with the same error as soon as I navigate to them.
I have tried adding this in my subclassed UIPageViewController as advised here, but it does not help either:
- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
[self.view layoutIfNeeded];
}
I suspect it is a bug with _UIPageViewControllerContentView's implementation, but I don't know for sure, and have not figured out how to swizzle its layoutSubviews method (like here) since it is a private class.
Here is how I add my page view controller to its parent:
self.myPageViewController = [[MyPageViewController alloc] initWithObject:object];
[self addChildViewController:self.myPageViewController];
self.myPageViewController.view.translatesAutoresizingMaskIntoConstraints = NO;
[self.containerView addSubview:self.myPageViewController.view];
[self.myPageViewController didMoveToParentViewController:self];
NSDictionary *viewsDictionary = @{@"pageView" : self.myPageViewController.view};
[self.containerView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[pageView]-0-|" options:0 metrics:nil views:viewsDictionary]];
[self.containerView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[pageView]-0-|" options:0 metrics:nil views:viewsDictionary]];
I would be thrilled if anyone had a solution to this problem. If any other details may help, I'd be happy to supply them here. Thanks.