Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is systemLayoutSizeFittingSize returning (0, 0) for UITableViewCell?

Here is the code snippet

    self.xyzIcon = [UIImage mobileImageNamed:@"icon_xyz"];
    self.xyzIconImageView = [UIImageView new];
    [self.contentView addSubview:self.xyzIconImageView];

    [self.xyzIconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.contentView.mas_left);
        make.width.equalTo(@(weakSelf.xyzIcon.size.width));
        make.height.equalTo(@(weakSelf.xyzIcon.size.height));
    }];

In the view controller, which uses tableview

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (!self.prototypeCell) {
        self.prototypeCell = [UITableViewCell new];
    }

    [self.prototypeCell configure];

    CGFloat heightCalculated = [self.prototypeCell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height + 1.f;

    NSLog(@"Height calculated %f", heightCalculated);

    returns heightCalculated; 
}

This always returns 1.f. Please help and I am not sure what I am doing wrong here.

like image 905
mobjug Avatar asked Oct 28 '14 00:10

mobjug


1 Answers

The problem is that your constraints are not determinative of the height of the cell. You have not pinned the top of the image view or the bottom of the image view to its superview (the contentView). So the cell has nothing internal to keep it from collapsing all the way to zero, as it were, when you call systemLayoutSizeFittingSize.

To put it another way: the way systemLayoutSizeFittingSize works is by treating something's internal constraints as struts that give it a minimum (or maximum) size. You have no struts.

like image 93
matt Avatar answered Sep 28 '22 09:09

matt