0
私は自分のコードにいくつかのボタンのレイアウトを調整する機能を持っています。これは、.hiddenが設定されているときに呼び出されます。プログラムで制約を調整するには、まずそれらを削除してから再追加する必要がありますか?
private func layoutButtons() {
redButton.hidden = !redButtonEnabled
redButtonLabel.hidden = !redButtonEnabled
yellowButton.hidden = !yellowButtonEnabled
yellowButtonLabel.hidden = !yellowButtonEnabled
removeConstraint(yellowButtonTrailingContraint)
if yellowButtonEnabled && !redButtonEnabled {
yellowButtonTrailingContraint = NSLayoutConstraint(item: yellowButton, attribute: .Trailing, relatedBy: .Equal, toItem: self, attribute: .Trailing, multiplier: 1.0, constant: -horizontalMargin)
} else {
yellowButtonTrailingContraint = NSLayoutConstraint(item: yellowButton, attribute: .Trailing, relatedBy: .Equal, toItem: redButton, attribute: .Leading, multiplier: 1.0, constant: -horizontalMargin)
}
addConstraint(yellowButtonTrailingContraint)
}
制約を変更する前に削除してから、上記のように後で再追加する必要がありますか?どこかの例でこれを見たが、ちょっと変わっているようだ。これについての指針は本当に感謝しています。ありがとう!
あなたの答えをありがとう!私を助けてくれました。上記の私の例は、削除して再度追加する必要はありませんか?もちろん、layoutIfNeededを呼び出すだけで十分です。 – Kex
実行時に@Kex制約を削除するとほとんどの場合パフォーマンスが低下するため、お勧めしません。無効化または単純に一定値(または乗数)を変更することが最初に行われるべきです。 – slxl