From my testing and this answer here , the UIView returned from this method automatically has the origin equal to (0, 0) , its height set to the value returned from -tableView: heightForHeaderInSection: and its width is set equal to the width of the UITableView .
I managed to add controls to this UIView and even lay them out using an automatic layout without specifying any specific size in the init method.
Here is my code to create a header view:
self.headerView = [[UIView alloc] init];
Here is the code in which I place the controls inside the header:
- (void)layoutControls { [self.headerView addSubview:self.segmentedControl]; [self.headerView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-(margin)-[control]-(margin)-|" options:0 metrics:@{@"margin": @(self.segmentedControlLeftRightMargin)} views:@{@"control": self.segmentedControl}]]; [self.headerView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-(margin)-[control(==height)]" options:0 metrics:@{@"margin": @(self.segmentedControlTopMargin), @"height": @(self.segmentedControlHeight)} views:@{@"control": self.segmentedControl}]]; [self.headerView addSubview:self.searchBar]; [self.headerView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-(margin)-[control]-(margin)-|" options:0 metrics:@{@"margin": @(self.searchBarLeftRightMargin)} views:@{@"control": self.searchBar}]]; [self.headerView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[control1]-(margin1)-[control2]-(margin2)-|" options:0 metrics:@{@"margin1": @(self.segmentedControlBottomMargin), @"margin2": @(self.searchBarBottomMargin)} views:@{@"control1": self.segmentedControl, @"control2": self.searchBar}]]; }
Here are the methods for the UITableViewDelgate protocol:
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
source share