ViewForHeaderInSection autolayout - pin width

I use the delegate method of the UITableView viewForHeaderInSection to provide the section title in my UITableView .

First, I create a view like this:

 UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 50)]; 

Then add some routines using Autolayout, then return headerView

I have a problem: I do not want to specifically specify the size of the headerView . I want to use Autolayout to snap left and right edges to the width of the view. Here's the problem, I don't have a supervisor to use in Autolayout code.

Using the code above means that the title view does not freeze during rotation. You must reload the table view after rotation.

Any ideas on how I can set the headerView to snap its edges to the table view?

thanks

+5
source share
1 answer

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 { // The hard-coded values are accurate for my controls, but you might need more advanced logic return 44.0f + self.segmentedControlBottomMargin + 44.0f + self.searchBarBottomMargin; } - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { return self.headerView; } 
+2
source

Source: https://habr.com/ru/post/1210729/


All Articles