这真的很奇怪。
无论我如何设置约束,它都会在布局后忽略所有约束。
我尝试使用 cell.indicator.center = cell.center
和 cell.indicator.autoresizingMask = [.flexibleLeftMargin, .flexibleRightMargin, .flexibleTopMargin, .flexibleBottomMargin]
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
但仍然没有用。
有什么特殊的方法可以让 UITableViewCell
中的 subview 居中吗?
加载单元格.xib
截图(模拟器)
更新:
在尝试了@Vibha Singh 的建议后,我将这些代码放入我的 LoadingCell
中。
override func layoutSubviews() {
print("layoutSubviews")
print(center)
print(contentView.bounds)
indicator.center = contentView.center
}
它打印了这些行:
layoutSubviews
(207.0, 1055.16668891907)
(0.0, 0.0, 414.0, 44.6666666666667)
但是指标还是没有居中。
更新:
我通过创建一个带有指示器的新单元格来修复它。 它们都具有完全相同的约束。新的按预期居中,但旧的仍然位于左上角。
更新:
我第一次确实使用了centerX和centerY作为约束。但它没有用。所以我必须尝试另一种方法。这就是我在第一个屏幕截图中使用这么多约束的原因。
这是两个完全相同的 xibs 的屏幕截图。
他们都使用相同的代码出队。
let dequeue = tableView.dequeueReusableCell(withIdentifier: SharedCell.loading.rawValue, for: indexPath)
if let cell = dequeue as? CenterLoadingCell {
cell.indicator.startAnimating()
}
else if let cell = dequeue as? LoadingCell {
cell.indicator.startAnimating()
}
return dequeue
第一个名为LoadingCell
,不以模拟器为中心。
第二个名为CenterLoadingCell
,是我在提出这个问题后创建的。而这个以模拟器为中心。
最佳答案
关于iOS - subivew 不能在 UITableViewCell 中居中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44652276/