将视图动态移动到自动布局中的新位置
Dynamically move a view to a new position in auto layout
我有一个看起来像这样的自定义 UITableViewCell
布局。它有三个标签。
标签2是可选的。它不存在于每个细胞中。所以我想隐藏它并在发生这种情况时将 Label 1 向下移动一点以与 Label 3 居中对齐。
这是我为每个标签添加的约束条件。
标签 1
标签 2
标签 3
注意我已经添加了一个额外的约束,将中心与 Y 对齐,值为 0 到 Label 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 先生的 ,我向 标签 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
}
但这也没有用。
尝试为标签 1 的顶部约束设置一个出口。当您删除标签 2 时,更新作为容器的标签 1 的顶部约束。height/2 或者删除顶部约束并将 centerY 约束赋予标签 1。并在更新约束后根据需要进行布局。
我对你到目前为止的方式印象深刻,因为如果我必须完成它,那将是完全相同的步骤:)
现在我的建议是:
删除您添加的没有任何用途的额外 "Align center to Y with the value of 0 to Label 1" :)
我可以看到你已经有一个与 y 对齐的中心,我相信 -13 到标签 1 有一些偏移量。为此创建一个 iboutlet :) 假设它的名称为 centerLabel1Constraint :)
每当您想将标签 1 置于中心隐藏标签 2 并设置 centerLabel1Constraint.constant = 0 并调用 [cell layoutIfNeeded]
应该可以了:)
编码愉快:)
我找到了一种方法,利用 NSLayoutConstraint
中的新 active
属性,如 answer 中所述。
我对值 -13 的对齐中心 Y 约束做了 IBOutlet
。从中删除了 weak
关键字。
然后在 cellForRowAtIndexPath
方法中,我只是切换 active
属性.
的值
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! CustomCell
if indexPath.row % 2 == 0 {
cell.label2.hidden = true
cell.oldConstraint.active = false
} else {
cell.label2.hidden = false
cell.oldConstraint.active = true
}
return cell
}
我有一个看起来像这样的自定义 UITableViewCell
布局。它有三个标签。
标签2是可选的。它不存在于每个细胞中。所以我想隐藏它并在发生这种情况时将 Label 1 向下移动一点以与 Label 3 居中对齐。
这是我为每个标签添加的约束条件。
标签 1
标签 2
标签 3
注意我已经添加了一个额外的约束,将中心与 Y 对齐,值为 0 到 Label 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 先生的 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
}
但这也没有用。
尝试为标签 1 的顶部约束设置一个出口。当您删除标签 2 时,更新作为容器的标签 1 的顶部约束。height/2 或者删除顶部约束并将 centerY 约束赋予标签 1。并在更新约束后根据需要进行布局。
我对你到目前为止的方式印象深刻,因为如果我必须完成它,那将是完全相同的步骤:)
现在我的建议是: 删除您添加的没有任何用途的额外 "Align center to Y with the value of 0 to Label 1" :)
我可以看到你已经有一个与 y 对齐的中心,我相信 -13 到标签 1 有一些偏移量。为此创建一个 iboutlet :) 假设它的名称为 centerLabel1Constraint :)
每当您想将标签 1 置于中心隐藏标签 2 并设置 centerLabel1Constraint.constant = 0 并调用 [cell layoutIfNeeded]
应该可以了:) 编码愉快:)
我找到了一种方法,利用 NSLayoutConstraint
中的新 active
属性,如 answer 中所述。
我对值 -13 的对齐中心 Y 约束做了 IBOutlet
。从中删除了 weak
关键字。
然后在 cellForRowAtIndexPath
方法中,我只是切换 active
属性.
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! CustomCell
if indexPath.row % 2 == 0 {
cell.label2.hidden = true
cell.oldConstraint.active = false
} else {
cell.label2.hidden = false
cell.oldConstraint.active = true
}
return cell
}