Senior Senior - 5 months ago 47
iOS Question

UIPageViewController + Auto Layout rotation issue

I have a UIPageViewController with a simple UIViewController inside of it.

If the the UIViewController has no subviews, its view resizes correctly when rotated. (Solid green background).

portrait landscape

If the the UIViewController has a subview with a fixed frame, its view resizes correctly when rotated. (Solid green background with yellow square in corner).

portrait landscape

If the the UIViewController has a subview with its auto layout constraints set to fill its superview, its view no longer resizes correctly when rotated. (Solid yellow background with the UIPageViewController's red background visible). The auto layout code I use is:

UIView *v = [[UIView alloc] init];
[v setTranslatesAutoresizingMaskIntoConstraints:NO];
[v setBackgroundColor:[UIColor yellowColor]];
[[self view] addSubview:v];

NSDictionary *viewsDictionary = NSDictionaryOfVariableBindings(v);

NSArray *cs = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[v]|" options:0 metrics:nil views:viewsDictionary];
[[self view] addConstraints:cs];

cs = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[v]|" options:0 metrics:nil views:viewsDictionary];
[[self view] addConstraints:cs];


portrait landscape

Why would the auto layout of a subview affect its superview? This only happens if it is contained within a UIPageViewController.

Answer

I figured it out:

I had called [[_pageViewController view] setTranslatesAutoresizingMaskIntoConstraints:NO]; in the container controller class and not explicitly set the auto layout constraints on its view (or views, as Bored Astronaut mentioned).