我需要帮助!我如何计算位于tableView单元格中的视图的纵横比,此视图应显示不同的设备(17:10)。我有一个包含一定数量单元格的表格。我将任意表格的单元格长度设置为226.0。这样做是为了显示单元格之间24个点的任意距离。(无故事板)
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 226.0
}
在单元格内部,我有一个高度为(17:10)的视图,我使用SnapKit作为约束。
cellView.snp.makeConstraints { make in
make.top.equalTo(self.contentView.snp.top)
make.leading.equalTo(self.contentView.snp.leading)
make.trailing.equalTo(self.contentView.snp.trailing
make.height.equalTo(self.contentView.snp.width).multipliedBy(10.0 / 17.0)
}
对于测试,我使用iPhone SE(第三代),运行应用程序后,长宽比只适用于此模型,它有24点之间的距离细胞.如果你运行其他模拟器模型,如iPhone 14 Pro max,细胞粘在一起.我怎么计算,使不同的模型之间的距离始终保持24点?
- 编辑**
SE第3代模拟器结果
正如你在截图中看到的,这个模拟器对应的长宽比是17:10,加上其他单元格之间的距离是24点。但是如果你运行一个更大的模型的模拟器,那么单元格会粘在一起。
1条答案
按热度按时间ajsxfq5m1#
你想让单元格设置自己的高度,所以...
彻底摆脱这个:
按如下方式配置单元格类:
下面是一个基本的表视图控制器,可以查看结果:
输出:
第一节第一节第一节第一节第一次
编辑- * 以阐明约束优先级... *
在单元格(和其他视图)中使用动态大小约束时,一个非常常见的情况是最终得到一个不太有效的值。
例如,在aniPhone 14 Pro(
cellView
,每边有12个点)上运行此命令:393
cellView
子视图的实际宽度为369
(393 - 24)自动布局将向调试控制台打印警告,因为它无法设置
217.05882352941177
的高度...它将使用217
。通过给Bottom约束一个小于所需的优先级,我们告诉自动布局我们可以使用
217
(也就是说,它是***不需要的***)。这是一个更好的makeConstraints块(我们不需要降低顶部约束的优先级),并且上面的代码使用了
contentView
宽度而不是cellView
宽度,所以它的长宽比不是17:10: