Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Auto Layout still required..." crash when returning to main view controller

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:

  1. 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.

  2. 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??

like image 780
LarrySnyder610 Avatar asked Aug 28 '14 20:08

LarrySnyder610


1 Answers

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

like image 155
BFar Avatar answered Nov 15 '22 08:11

BFar