Auto layout invalid after presenting view controller (and many situation)

Michael Hui picture Michael Hui · Jul 16, 2013 · Viewed 20.4k times · Source

Background:

I have a custom container controller, which just like UITabBarController, but with switch animation. And I use auto layout to achieve.
Because switch between child controllers is dynamic, the proper constraints are added to child view controller when its's view is added into container's view, not pre-setup in IB. (The constraints are added to the superview of course)

http://d.pr/i/q6NF Container Controller Nib Setup

PS: The detail of constraints
H:|[Child](Change the constraint's constant to animation from left/right to right/left)
H:[Child(==Super)]
V:|[Child]|

Where is the Problem:

One of the child controller is a navigation controller, things go wrong when the navigation controller present a modal view controller (using presentViewController:animated:completion:), and dismiss it(usingdismissViewControllerAnimated:completion:), after present/ dismiss the frame of navigation controller become (x, y, 0, 0), It seems like the auto layout becomes invalid, maybe the constraints been removed.

http://d.pr/i/VmvL The Present/ Dismiss Process

Research

I haven't use code to verify what happen to these constraints yet, but with Spark Inspector I see the views presentation change during the present/ dismiss process. When My navigation controller present a modal view controller, iOS just swap the whole navigation controller's view to the modal view controller's view. And when navigation controller's view come back, the auto layout doesn't work anymore.

Possible Solution

One of the solution I came up with is let my container controller present modal controller.

Or I just change my container controller to no auto layout.

The fact is ever since I start using auto layout, the problems this technique causes just dominate the benefits. Besides this problem, Every time the interface orientation changes, views inside my container controller just can't auto layout right, it seems subviews always use the frame of superview before orientation change. I double check the constraints I setup, there is no conflicts and no ambiguity.

My guess is that my custom container controller isn't compatible with presenting modal view controller and interface orientation change in Auto Layout system, even with constraints setup.

Xcode Environment

Xcode 5 beta, iOS 7SDK, target iOS6.1 Maybe Something not right with the SDK environment?

Answer

Palimondo picture Palimondo · Sep 6, 2013

I had a similar problem. I was setting translatesAutoresizingMaskIntoConstraints = NO; on my root UIView. It appears the "outermost" UIView - the superview at the root of your hierarchy must use the default translatesAutoresizingMaskIntoConstraints = YES. Once I've removed this, everything worked as expected.