UIViewTableCell中的layoutSubviews不起作用

Han*_*nXu 5 label position objective-c ios

我有一个类PostListTableCell,它继承了UITableViewCell,它用作UITableView中的自定义单元格。

我需要调整单元格中某些标签的大小,因此我在

- (void)layoutSubviews {
    [super layoutSubviews];
    [_titleLabel sizeToFit];
}
Run Code Online (Sandbox Code Playgroud)

但是问题是,当加载UITableView时,_titleLabel不会调整大小:

在此处输入图片说明

但是,当我单击此单元格并选择它后,标题的大小确实调整了:

在此处输入图片说明

以下是加载数据的代码:

- (PostListTableCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *PostListTableCellIdentifier = @"PostListTableCell";
    PostListTableCell *cell = (PostListTableCell*) [tableView dequeueReusableCellWithIdentifier:PostListTableCellIdentifier];

    if (cell == nil) {
        cell = [[PostListTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:PostListTableCellIdentifier];
    }

   Post *post = (Post*)_posts[indexPath.row];
    [cell loadPost:post];

    return cell;
}
Run Code Online (Sandbox Code Playgroud)

有人可以帮忙吗?

Mir*_*ano 2

问题是自动布局会根据您在 Interface Build 中制作标签时设置的约束再次调整标签大小,不幸的是在自动布局下,层次结构是Constraints > Frame