我有一个使用自动布局的 UITableView。我有多种类型的单元格,但现在让我们使用最简单的单元格,它只有一个 UILabel。
在 tableView:heightForRowAtIndexPath:
中,我有以下代码:
[self configureCell:self.detailsCell forRowAtIndexPath:indexPath];
[self.detailsCell layoutIfNeeded];
CGSize size = [self.detailsCell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
return size.height + 1;
依次调用
- (void)configureCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
// ...
cell.detailsLabel.text = self.data.myText;
cell.detailsLabel.font = [UIFont preferredFontForTextStyle:UIFontTextStyleBody];
// [cell.detailsLabel sizeToFit]; <- I tried this, but didn't help
}
我最终得到了这个:
绿色矩形是实际的单元格。紫色矩形是 UILabel。我不想要顶部和底部的 purple 填充/边距。我怎样才能摆脱它?
最佳答案
你必须提防“Constrain to margins:”
比较这两张图片: 和
因此在添加新约束时取消选择“constrian to margins”应该可以解决您的填充/边距问题:-)
如果您愿意,可以使用“Editor/Canvas/Show Layout Rectangles”可视化这些边距 没有布局矩形的 View : 和布局矩形设置为显示(这很好地说明了你的问题)
关于ios - 使用自动布局时删除 UILabel 填充/边距,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26289579/