11

I want to use an UISearchDisplayController on UIViewController, that includes an UITableView.
I use Autolayout. When i try to put the SearchBar (_searchBar) in the tableHeaderView with

self.tableView.tableHeaderView = _searchBar;

I get the error 'NSInternalInconsistencyException', reason: 'Auto Layout still required after executing -layoutSubviews. UITableView's implementation of -layoutSubviews needs to call super.'

Disabling Autolayout, the error disappears, but i need Autolayout...

The error appears, when I use Custom Cells or StandardCells...

The error appears, when the TableView has no rows...

elp
  • 8,021
  • 7
  • 61
  • 120
Ulli H
  • 1,748
  • 1
  • 19
  • 32

2 Answers2

16

As a general solution to this problem, or at least a way of finding the cause of it:

  • Turn on exception breakpoints
  • Subclass UITableView and override layoutSublayersOfLayer:, just calling super
  • Run your app - you will stop in your new method
  • In the debugger, type po [self _autolayoutTrace]

This will show you a printout of every view in the window, with the views where auto layout has not been able to come up with a solution highlighted by asterisks or AMBIGUOUS LAYOUT. These are the views you need to investigate the constraints for.

jrturton
  • 118,105
  • 32
  • 252
  • 268
  • In Swift projects: `expr -l objc++ -o -- [[UIWindow keyWindow] _autolayoutTrace]` (source: https://carpeaqua.com/2015/07/07/auto-layout-debugging-in-swift/) – Johan Kool Jul 13 '17 at 09:05
6

When adding a subview to UITableView there are some specific requirements for the subview. Consider adding the subview to another view(superview) in your controller instead of UITableView.

"Auto Layout still required after executing -layoutSubviews" with UITableViewCell subclass

Community
  • 1
  • 1
Vladimír Slavík
  • 1,727
  • 1
  • 21
  • 31
  • 1
    As @Vladimir points in his answer, autolayouting a view in a tableView will likely trigger that error (in the table itself, or even autolayouting a tableViewHeader/Footer). Just as a clarifying note, I see that this error might not appear for some layouts in iOS 8, but will certainly appear in iOS 7. – fbeeper Feb 25 '15 at 01:19