2017-06-20 18 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)