我有一個自定義UITableViewCell
佈局,看起來像這樣。它有三個標籤。動態地將視圖移動到自動佈局中的新位置
標籤2是一個可選的一個。它不存在於每個細胞中。所以我想隱藏和移動標籤1調低一點是中心與標籤3當這種情況發生對齊。
下面是我添加每個標籤的限制。
標籤1
標籤2
標籤3
通知我已經加入一個額外的約束,對齊中心至Y爲0的值,以標籤1並其優先級設置爲750。我想如果除去標籤2,該約束用低優先級將取代它的位置並向下移動。
class TableViewController: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
// MARK: - Table view data source
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 3
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! CustomCell
if indexPath.row == 1 {
cell.label2.removeFromSuperview()
cell.updateConstraints()
}
return cell
}
}
但它似乎沒有工作。 標籤2被刪除,但標籤1的位置仍然相同。
我怎樣才能做到我所追求的?
嘗試#1
按低於T先生的answer,我添加了一個頂部約束到標籤1。然後在cellForRowAtIndexPath
方法中,我改變了它的值。
override func tableView(tableView: UITableView, cellForRowAtIndexPath
indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! CustomCell
if indexPath.row == 1 {
cell.label2.removeFromSuperview()
cell.topConstraint.constant = cell.bounds.height/2
cell.layoutIfNeeded()
}
return cell
}
但是這也沒有工作。
對'layoutIfNeeded'一試? – zcui93
@ zcui93剛試過。不用找了。 – Isuru
我注意到標籤1的'CentreY'約束都是針對'superview'的,如果第二個涉及標籤2? – zcui93