How to change custom width viewForHeaderInSection after rotation

I need to create a custom tableViewHeader in which at least two shortcuts should be displayed, namely: project_name and project description. The contents of these two labels change (i.e. in terms of line length).

I managed to get a working version for the device’s default orientation (portrait), but if the user rotates the device to the landscape, the width of my user header does not change and the user sees unused empty space on the right.

The following code snippet is used:

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { return 50.0f; } - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { CGFloat wd = tableView.bounds.size.width; CGFloat ht = tableView.bounds.size.height; UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0., 0., wd, ht)]; headerView.contentMode = UIViewContentModeScaleToFill; // Add project name/description as labels to the headerView. UILabel *projName = [[UILabel alloc] initWithFrame:CGRectMake(5., 5., wd, 20)]; UILabel *projDesc = [[UILabel alloc] initWithFrame:CGRectMake(5., 25., wd, 20)]; projName.text = @"Project: this project is about an interesting .."; projDesc.text = @"Description: a very long description should be more readable when your device is in landscape mode!"; [headerView addSubview:projName]; [headerView addSubview:projDesc]; return headerView; } 

I noticed that tableView:viewForHeaderInSection: will only be called once after viewDidLoad , but not after changing the orientation of the device.

Should I implement the willRotateToInterfaceOrientation: method with something like:

 - (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration { [self.tableView reloadSections:nil withRowAnimation:UITableViewRowAnimationNone]; } 

I can't figure out how to make reloadSections: work by forcing recalculating my customView.

+4
source share
1 answer

If you are using autoresizingMask, the title is correctly set when rotating. Do not overestimate anything else. I would put the machines at the end of the code before returning the view:

  [headerView addSubview:projName]; [headerView addSubview:projDesc]; projName.autoresizingMask = UIViewAutoresizingFlexibleRightMargin; projDesc.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin; headerView.autoresizingMask = UIViewAutoresizingFlexibleWidth; return headerView; 
+7
source

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


All Articles