16

I am trying to add a UIView to a UITableView's header, then, using the NSLayoutConstraints I want to give it a height.

I've looked through the Apple Docs and the WWDC 2012 videos but I cannot find this particular error anywhere!

I have the following code:

- (UIImageView *)logoImageView
{
    if (!_logoImageView) {
        _logoImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"logo"]];
        [_logoImageView setTranslatesAutoresizingMaskIntoConstraints:NO];
    }
    return _logoImageView;
}

... in viewDidLoad

UIView *tableHeaderView = [[UIView alloc] init];
tableHeaderView.translatesAutoresizingMaskIntoConstraints = NO;

[tableHeaderView addSubview:self.logoImageView];

[self.tableView setTableHeaderView:tableHeaderView];

NSDictionary *constraintViews = @{@"tableView" : self.tableView, @"tableHeaderView" : tableHeaderView, @"logoImageView" : self.logoImageView};

[self.tableView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[tableHeaderView(50)]"
                                                                       options:0
                                                                       metrics:nil
                                                                         views:constraintViews]];

However when I run it I get the following error:

2012-10-08 16:31:34.559 myApp[6934:c07] *** Assertion failure in -[UITableView layoutSublayersOfLayer:], /SourceCache/UIKit_Sim/UIKit-2372/UIView.m:5776
2012-10-08 16:31:34.561 myApp[6934:c07] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Auto Layout still required after executing -layoutSubviews. UITableView's implementation of -layoutSubviews needs to call super.'
*** First throw call stack:
(0x199b012 0x17c0e7e 0x199ae78 0x1256f35 0x7589ef 0x17d46b0 0x622fc0 0x61733c 0x622eaf 0x7f78cd 0x7401a6 0x73ecbf 0x73ebd9 0x73de34 0x73dc6e 0x73ea29 0x741922 0x7ebfec 0x738bc4 0x738dbf 0x738f55 0x741f67 0x705fcc 0x706fab 0x718315 0x71924b 0x70acf8 0x27e8df9 0x27e8ad0 0x1910bf5 0x1910962 0x1941bb6 0x1940f44 0x1940e1b 0x7067da 0x70865c 0x6d5d 0x2395)
libc++abi.dylib: terminate called throwing an exception
Adam Carter
  • 4,741
  • 5
  • 42
  • 103

4 Answers4

3

I had the same exception when I was trying to set the table header view. When I noticed that the view had Contraints I just unchecked the "Use Autolayout" option from Storyboard.

Screenshot: https://i.stack.imgur.com/5jWHy.png

This solved for me.

idiogo
  • 193
  • 3
  • 5
  • 8
    This is just working around the problem, not fixing it :( You can't just turn off autolayout! – jgvb Feb 06 '14 at 03:26
2

The best way to overcome this is to use Interface Builder to setup the UITableView header, then add an Outlet to the height NSLayoutConstraint.

Adam Carter
  • 4,741
  • 5
  • 42
  • 103
  • 3
    Can you elaborate. No idea what this means. – jgvb Feb 06 '14 at 03:26
  • `NSLayoutConstraint`s are regular objects, so you can create outlets for them from Interface Builder, and manipulate their properties at runtime just as you would for an IB view (for instance) – Jonathan Crooke Jun 06 '14 at 14:22
  • I don't see a way to add height or width layout constraint to `UITableView`'s header or footer in storyboard. `Add New Constraints` has everything blocked inside. – derpoliuk Jan 18 '16 at 07:11
1

Try this:

logoImageView.translatesAutoresizingMaskIntoConstraints = YES;

(If not works, try to remove:

tableHeaderView.translatesAutoresizingMaskIntoConstraints = NO;  //Remove this line

:)

Soul Clinic
  • 1,189
  • 12
  • 18
0

I didn't get any proper solution for this issue but you can fix it by using frames and not setting translatesAutoresizingMaskIntoConstraints property to No (by default its yes, so don't set it)

CGRect headerViewFrame = CGRectMake(0,0,320,60); //Frame for your header/footer view

UIView *tableHeaderView = [[UIView alloc] initWithFrame:headerViewFrame];
tableHeaderView.translatesAutoresizingMaskIntoConstraints = Yes; //Or don't set it at all
[self.tableView setTableHeaderView:tableHeaderView];
sanjana
  • 3,034
  • 2
  • 25
  • 31