ios - UITableViewCell 中的 UILabel 约束横向变化

标签 ios autolayout uitableview

我有一个静态 UITableView在我的 Storyboard中,它结合了系统单元和自定义单元。为了使自定义单元格看起来像系统单元格,我从 UILabel 的前面添加了一个 15 点约束。到单元格的 contentView (以匹配 15 点的默认分隔符插入)。

这种策略在 table view 是纵向时效果很好,但在横向时看起来约束会缩小,如下所示:

肖像:
System cell alongside custom cell in portrait

景观:
System cell alongside custom cell in landscape

你可以在上图中看到“BUG REPORTING”部分标题,“Report a Bug”标签(由系统单元格定位),两个单元格之间的线都距离contentView的左侧15个点,但是“记录”标签更接近。

我尝试将标签限制在 contentView 上,边距打开和关闭,结果是一样的。当自定义标签的约束与 contentView 左侧的距离明显相同时,它们甚至会报告不同的常量值。

Example of constraint information when constrained to margin

Example of constraint information when not constrained to margin

有谁知道发生了什么?

最佳答案

尝试创建 IBOutlet对于标签的前导约束,并在显示单元格之前对其进行修改:

func tableView(_ tableView: UITableView, willDisplayCell cell: UITableViewCell, 
    forRowAtIndexPath indexPath: NSIndexPath) {
        let leftInset = cell.separatorInset.left
        cell.labelLeadingConstraint.constant = leftInset - 8
        // - 8.0 accounts for constraint to margins in autolayout
}

或者,您可以使用尺寸类为 Compact Height 尺寸类设置不同的约束:

Compact height size class

然后,只需将约束的常量设置为 12 而不是 7(或 20 而不是 15,如果不使用边距)。

关于ios - UITableViewCell 中的 UILabel 约束横向变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35787901/

相关文章:

ios - 如何快速获取选定的单元格索引路径

iOS - 在运行时找出 View 的约束

ios - 为什么当我相对于 super View 的底部进行约束时,它是从 super View 的顶部进行操作的?现在是这样吗?

ios - 获取 UIButton 在 UITableViewCell 中的位置

ios - UICollectionView Controller 和在Prepare for Segue 中传递 CloudKit 数据

ios - Moya 在 stub 和普通请求之间切换

ios - 在 iPad 屏幕中使用自动布局时按钮宽度会变长?

ios - 如何在 iOS 中滑动 UITableViewCell

ios - 在 objective-c 中保持 tableview 单元格上按钮的状态单击

ios - swift 3 和 Alamofire : Extremely Slow Build Times