ios - 使用自动布局时删除 UILabel 填充/边距

标签 ios objective-c uitableview interface-builder autolayout

我有一个使用自动布局的 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
}

Interface builder screenshot

我最终得到了这个:

Actual cell

绿色矩形是实际的单元格。紫色矩形是 UILabel。我不想要顶部和底部的 purple 填充/边距。我怎样才能摆脱它?

最佳答案

你必须提防“Constrain to margins:

比较这两张图片: Constrain to margins = YESConstrain to marings = NO

因此在添加新约束时取消选择“constrian to margins”应该可以解决您的填充/边距问题:-)

如果您愿意,可以使用“Editor/Canvas/Show Layout Rectangles”可视化这些边距 没有布局矩形的 View : Show Layout rectangles = NO 和布局矩形设置为显示(这很好地说明了你的问题) Show Layout rectangles = YES

关于ios - 使用自动布局时删除 UILabel 填充/边距,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26289579/

相关文章:

objective-c - 错误 : ‘NSString’ undeclared (first use in this function)

ios - 当我运行应用程序时,我的 TableView 崩溃了

即使我正在释放,iPhone 应用程序也会由于收到内存警告而崩溃

ios - 为基于 iOS 的项目实现 MVC vs MVVM vs VIPER 的真实场景

ios - isKindOfClass 为测试用例返回不同的值

ios - UICollectionViewFlowLayout 的 estimatedItemSize 中断滚动?

ios - 尝试从包复制到文档目录时出错

iphone - UITableViewCell 编辑缩进

ios - reloadRowsAtIndexPaths 搞乱了 animateWithDuration

ios - UIDatePicker 返回当前日期未选择的日期