这真的很奇怪。 无论我如何设置约束,它只是在布局后忽略所有这些约束。 我试图在func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
中使用cell.indicator.center = cell.center
和cell.indicator.autoresizingMask = [.flexibleLeftMargin, .flexibleRightMargin, .flexibleTopMargin, .flexibleBottomMargin]
,但仍无法使用。 有没有什么特别的方法可以在一个UITableViewCell
的子视图中居中?iOS - subivew不能居中在UITableViewCell中
LoadingCell.xib
截图(模拟器)
更新:
试图@Vibha辛格的意见后,我把这些代码在我的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
,我创建后,我问这个问题。而这个以模拟器为中心。
您可以添加有关约束的详细信息您正在使用? 'cellForRow'中的设置不起作用,因为在此之后有更多布局完成 –
使用'cell.indicator.center = cell.contentView?.center'尝试使用'cell.indicator.center = cell.center ' –
@CatalinaT。您可以在第一张图片中看到所有约束条件。 –