My app launches with a main menu which is a custom subclass of UIViewController
called LVSMainViewController
. It is embedded in a UINavigationController
which is set as the initial VC in a storyboard. LVSMainViewController
implements -viewDidLayoutSubviews
.
Tapping a button takes the user to a different section of the app (a different VC). The user returns to the main menu via a button wired to a push segue. But when the app loads the main menu VC again, it crashes with the message:
2014-08-28 16:11:14.122 * Assertion failure in -[UIView layoutSublayersOfLayer:], /SourceCache/UIKit_Sim/UIKit-2935.137/UIView.m:8803
2014-08-28 16:11:14.257 * Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Auto Layout still required after sending -viewDidLayoutSubviews to the view controller. LVSMainViewController's implementation needs to send -layoutSubviews to the view to invoke auto layout.'
Other posts on SO report the same error when setting auto-layout constraints programmatically (I am not doing that, though I am using auto-layout in the storyboard) and/or when using UITableView
(which I am not using in either the main menu VC or the VC it segues to, though I am using it elsewhere in the app). (See here or here.)
Other pieces of the puzzle:
I tried adding [self.view layoutSubviews];
at the end of -viewDidLayoutSubviews
. It doesn't crash when I do this. But it seems ill-advised since Apple's documentation says
You should not call this method directly. If you want to force a layout update, call the setNeedsLayout method instead to do so prior to the next drawing update.
If I instead add [self.view setNeedsLayout];
at the end of -viewDidLayoutSubviews
, the app crashes with it first loads the main menu VC, not when I leave it and return.
What might be causing this??
The reason the crash is magically fixed is likely because you are now using iOS 8. Have you tried to run this code on iOS7?
You are close to the answer by calling [self.view layoutSubviews]
.
Try calling [self.view layoutIfNeeded]
instead.
See these posts:
Autolayout and subviews
Auto Layout error
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With