UITableViewCell systemLayoutSizeFittingSize: returns 0 on iOS 7

I have a UITableViewCell with a UIImage fixed in the upper left and the label on the right:

-(UITableViewCell*) adCellFromTableView:(UITableView*)tableView { //Build the text NSString* adText = NSLocalizedString(@"MyFamily_fremiumAd", nil); NSMutableAttributedString* attrString = [[NSMutableAttributedString alloc] initWithString:adText]; NSUInteger newLineLocation = [adText rangeOfCharacterFromSet:[NSCharacterSet newlineCharacterSet]].location; //Set the first line in orange NSDictionary* firstLineAttributes = @{NSFontAttributeName:[UIFont systemFontOfSize:15], NSForegroundColorAttributeName:ORANGE}; [attrString addAttributes:firstLineAttributes range:NSMakeRange(0, newLineLocation)]; //Set other lines in white NSDictionary* otherLinesAttributes = @{NSFontAttributeName:[UIFont systemFontOfSize:11], NSForegroundColorAttributeName:[UIColor whiteColor]}; [attrString addAttributes:otherLinesAttributes range:NSMakeRange(newLineLocation, adText.length - newLineLocation)]; //Get the cell if (!adCell) { adCell = [tableUsers dequeueReusableCellWithIdentifier:@"fremiumAd"]; } //Set the text UILabel* label = (UILabel*)[adCell viewWithTag:1]; label.attributedText = attrString; //Hide the separator adCell.separatorInset = UIEdgeInsetsMake(0, adCell.bounds.size.width, 0, 0); return adCell; } -(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell* cell = [self adCellFromTableView:tableView]; //Make sure the cell bounds are the same as tableview before calculating the height //This is important when we calculate height after a UI rotation. cell.bounds = CGRectMake(0, 0, tableView.bounds.size.width, 0); NSLog(@"cell.bounds: %@",NSStringFromCGRect(cell.bounds)); [cell setNeedsLayout]; [cell layoutIfNeeded]; CGSize fittingSize = [cell systemLayoutSizeFittingSize:UILayoutFittingCompressedSize]; NSLog(@"fittingSize: %@",NSStringFromCGSize(fittingSize)); return fittingSize.height; } 

When I run the application on the iOS 7.1 simulator, systemLayoutSizeFittingSize always returns 0:

cell.bounds: {{0, 0}, {320, 0}}

fitSize: {0,0}

When I run the application on iOS 8.1 simulator, systemLayoutSizeFittingSize returns the correct value:

cell.bounds: {{0, 0}, {320, 0}}

fitSize: {320, 154.5}

What am I missing?

Edit: I somehow fixed the problem using [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize]; instead of [cell systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];

But this is only half the fix: when I rotate while the cell is visible, the size calculation is fine. But when I scroll the cell on the screen, rotate the user interface, and then scroll back to the cell, the height calculation is again incorrect in iOS 7.1

Here are the magazines before turning from landscape to portrait:

Table boundaries: {{0, 0}, {569, 227}}

cell.bounds: {{0, 0}, {569.0}}

cell.contentView: {{0, 0}, {569, 0}}

fitSize: {559, 162}

The following are the magazines after turning from landscape to portrait:

tableView bounds: {{0, 249}, {321, 463}}

cell.bounds: {{0, 0}, {321,0}}

cell.contentView: {{0, 0}, {321, 0}}

fitSize: {559, 162}

As you can see, the size calculation is the same no matter what the width of the cell /cell.contentView is.

This leads to an increase in the size of the cell when rotating from portrait to landscape and to a smaller cell when rotating from landscape to portrait.

+6
source share
1 answer

You are correct using cell.contentView instead of a cell in systemLayoutSizeFittingSize: method. Using auto-layout, all custom subqueries should only be added to the contents of cellView, as well as restrictions. Then, Auto Layout cells work well, as expected.

As your second question, I solved a similar problem. Paste the following code into your - (void) didRotateFromInterfaceOrientation: (UIInterfaceOrientation) fromInterfaceOrientation method, it may help you solve this problem.

 -(void) didRotateFromInterfaceOrientation: (UIInterfaceOrientation)fromInterfaceOrientation { // Update the layout for the new orientation //Maybe you should change it to your own tableview [self updateViewConstraints]; [self.view layoutIfNeeded]; // other any code ... } 
0
source

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


All Articles