2017-06-20 17 views
-1

这真的很奇怪。 无论我如何设置约束,它只是在布局后忽略所有这些约束。 我试图在func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell 中使用cell.indicator.center = cell.centercell.indicator.autoresizingMask = [.flexibleLeftMargin, .flexibleRightMargin, .flexibleTopMargin, .flexibleBottomMargin],但仍无法使用。 有没有什么特别的方法可以在一个UITableViewCell的子视图中居中?iOS - subivew不能居中在UITableViewCell中

LoadingCell.xib

enter image description here

截图(模拟器)

enter image description here

更新:

试图@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,这是不在模拟器上居中。

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here

第二个被命名为CenterLoadingCell,我创建后,我问这个问题。而这个以模拟器为中心。

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here

+0

您可以添加有关约束的详细信息您正在使用? 'cellForRow'中的设置不起作用,因为在此之后有更多布局完成 –

+0

使用'cell.indicator.center = cell.contentView?.center'尝试使用'cell.indicator.center = cell.center ' –

+0

@CatalinaT。您可以在第一张图片中看到所有约束条件。 –

回答

0

您可以添加指标像

let activityView = UIActivityIndicatorView(activityIndicatorStyle: .gray) 
          activityView.center = CGPoint(x: CGFloat(cell.bounds.midX), y: CGFloat(cell.bounds.midY)) 

          cell.contentView.addSubview(activityView) 
          activityView.startAnimating() 

也可以在电池下面的方法来设置帧

override func layoutSubviews() 
{ 
} 
1

通过添加不必要的约束,您可能会让布局设置时间混乱。检查这个实现。

Image with Constraint

随着定制手机: enter image description here

+0

试过。我第一次使用这些限制。但它不起作用。所以我尝试了另一种方式来看看它是否有效。 –

+0

我已经尝试过自定义单元实现太检查附加的另一个截图。 – rptwsthi

+0

刚刚创建了另一个带指示符的单元格,并且只使用centerX和centerY约束。这一个如预期的那样居中。但最初的'LoadingCell'仍然不居中。他们都有完全相同的限制。这是否意味着原始的'LoadingCell'在自动布局中存在内部问题? –

0

你可以用约束

func setConstraints(item:UIView , relatedTo:UIView , attr:NSLayoutAttribute , relatedBy:NSLayoutRelation , multiplier: CGFloat , constant : CGFloat) 

{ 
    relatedTo.addSubview(item) 
    item.translatesAutoresizingMaskIntoConstraints = false 
    NSLayoutConstraint(item: item, attribute: attr, relatedBy: relatedBy, toItem: relatedTo, attribute: attr, multiplier: multiplier, constant: constant).isActive = true 




} 

和使用使其很容易:

setConstraints(item: indicator, relatedTo:cell.contentView, attr:centerX, relatedBy:.equal, multiplier: 0 , constant : 0)