【问题标题】:Why is systemLayoutSizeFittingSize returning (0, 0) for UITableViewCell?为什么 systemLayoutSizeFittingSize 为 UITableViewCell 返回 (0, 0)?
【发布时间】:2014-10-28 00:05:17
【问题描述】:

这里是代码sn-p

    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));
    }];

在使用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; 
}

这总是返回 1.f。请帮忙,我不确定我在这里做错了什么。

【问题讨论】:

    标签: ios uitableview autolayout


    【解决方案1】:

    问题是您的约束不能决定单元格的高度。您尚未将图像视图的顶部或图像视图的底部固定到其父视图(contentView)。因此,当您调用 systemLayoutSizeFittingSize 时,单元格内部没有任何东西可以防止它一直崩溃到零。

    换一种说法:systemLayoutSizeFittingSize 的工作方式是将某事物的内部约束视为赋予其最小(或最大)大小的支柱。你没有支柱。

    【讨论】:

    • 这很有意义。但它仍然无法正常工作:(。我添加了这个 :make.top.equalTo(@0);
    • 嗯嗯。你读过我说的吗?您必须将图像视图的顶部和固定到内容视图。你没有那样做。你不听我帮不了你。
    • 好吧,我错过了底部。谢谢。
    • 当我添加一个底部约束时,它与另一个冲突,所以我通过将底部约束设为 999 优先级来解决它
    • 也谢谢你!你的回答很有帮助!
    猜你喜欢
    • 2015-01-14
    • 2022-01-23
    • 1970-01-01
    • 2015-06-21
    • 1970-01-01
    • 2011-03-01
    • 2011-07-01
    • 2011-01-17
    • 2018-04-05
    相关资源
    最近更新 更多