Since I discovered AutoLayout
I use it everywhere, now I'm trying to use it with a tableHeaderView
.
I made a subclass
of UIView
added everything (labels etc…) I wanted with their constraints, then I added this CustomView
to the UITableView
'tableHeaderView
.
Everything works just fine except the UITableView
always displays above the CustomView
, by above I mean the CustomView
is under the UITableView
so it can't be seen !
It seems that no matter what I do, the height
of the UITableView
'tableHeaderView
is always 0 (so is the width, x and y).
My question : is it possible at all to accomplish this without setting the frame manually ?
EDIT :
The CustomView
'subview
that I'm using has these constraints :
_title = [[UILabel alloc]init];
_title.text = @"Title";
[self addSubview:_title];
[_title keep:[KeepTopInset rules:@[[KeepEqual must:5]]]]; // title has to stay at least 5 away from the supperview Top
[_title keep:[KeepRightInset rules:@[[KeepMin must:5]]]];
[_title keep:[KeepLeftInset rules:@[[KeepMin must:5]]]];
[_title keep:[KeepBottomInset rules:@[[KeepMin must:5]]]];
I'm using a handy library 'KeepLayout' because writing constraints manually takes forever and way too many line for one single constraint but the methods are self-explaining.
And the UITableView
has these constraints :
_tableView = [[UITableView alloc]init];
_tableView.translatesAutoresizingMaskIntoConstraints = NO;
_tableView.delegate = self;
_tableView.dataSource = self;
_tableView.backgroundColor = [UIColor clearColor];
[self.view addSubview:_tableView];
[_tableView keep:[KeepTopInset rules:@[[KeepEqual must:0]]]];// These 4 constraints make the UITableView stays 0 away from the superview top left right and bottom.
[_tableView keep:[KeepLeftInset rules:@[[KeepEqual must:0]]]];
[_tableView keep:[KeepRightInset rules:@[[KeepEqual must:0]]]];
[_tableView keep:[KeepBottomInset rules:@[[KeepEqual must:0]]]];
_detailsView = [[CustomView alloc]init];
_tableView.tableHeaderView = _detailsView;
I don't know if I have to set some constraints directly on the CustomView
, I think the height of the CustomView is determined by the constraints on the UILabel
"title" in it.
EDIT 2: After another investigation it seems the height and width of the CustomView are correctly calculated, but the top of the CustomView is still at the same level than the top of the UITableView and they move together when I scroll.
Best Answer
I asked and answered a similar question here. In summary, I add the header once and use it to find the required height. That height can then be applied to the header, and the header is set a second time to reflect the change.
If you have multi-line labels, this also relies on the custom view setting the preferredMaxLayoutWidth of each label:
or perhaps more generally:
Update January 2015
Unfortunately this still seems necessary. Here is a swift version of the layout process:
I've found it useful to move this into an extension on UITableView:
Usage: