晚上,
ScrollView 约束有问题。
这是我的代码:
// when the keyboard is shown, move up some elements
@objc func keyboardWillShow(notification: NSNotification) {
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue {
// Bring up the scrol lview
print(scrollViewBottomConstraint.constant)
self.scrollViewBottomConstraint.constant = keyboardSize.height
print(scrollViewBottomConstraint.constant)
self.view.updateConstraints()
self.view.updateConstraintsIfNeeded()
self.view.needsUpdateConstraints()
self.view.layoutIfNeeded()
// Bring up the next arrow with animation
self.nextButtonBottomConstraint.constant = keyboardSize.height + 8
UIView.animate(
withDuration: 1.0,
delay: 0.0,
usingSpringWithDamping: 0.5,
initialSpringVelocity: 6.0,
options: [.allowUserInteraction,
.curveEaseInOut],
animations: {
self.view.layoutIfNeeded()
}, completion: nil)
}
}
控制台打印 0 和 253,所以更新了约束常量。但是在屏幕上我看不到变化,我有这个日志:
[Snapshotting] Snapshotting a view (0x7fa041471bc0, UIInputSetHostView) that has not been rendered at least once requires afterScreenUpdates:YES.
最佳答案
奇怪,更改约束后只需 view.layoutIfNeeded() 即可完成工作。
尝试删除:
self.view.updateConstraints()
self.view.updateConstraintsIfNeeded()
self.view.needsUpdateConstraints()
只是:
view.layoutIfNeeded()
试试这个:
@objc func keyboardWillShow(notification: NSNotification) {
guard let keyboardSize = (notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue else{
return
}
scrollViewBottomConstraint.constant = keyboardSize.height
nextButtonBottomConstraint.constant = keyboardSize.height + 8
view.layoutIfNeeded()
}
关于iOS swift : Constraints did not update on screen,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47157215/