这是我的代码,用于在键盘出现时将视图上移(还请注意,该视图具有CollectionView
):
if isKeyboardShowing {
UIView.animate(withDuration: animationDuration, animations: {
if self.isCollectionViewBlockingInput() {
self.view.frame.origin.y = -distanceShifted
// Keyboard is shifted less because view already shifts by distance
self.bottomConstraintForInput.constant = -keyboardViewEndFrame.height + distanceShifted
} else {
self.bottomConstraintForInput.constant = -keyboardViewEndFrame.height
}
})
}
本质上,视图的原点y向上移动了一定距离。这适用于iOS 10,但在iOS 9中,**当视图首次加载并且我第一次打开键盘时,视图会尝试向上移动但失败。取而代之的是,黑色背景似乎会瞬间移下,然后消失:
仅在滚动集合视图之前打开键盘会发生这种情况。一旦开始滚动收集视图,错误就会消失,键盘会再次正确向上移动。
同样,此错误根本不会在iOS 10中显示。
最佳答案
尝试这个
UIView.animate(withDuration: 1.0, animations: {
//Do your constraint change part here
}, completion: {bool in
// Call here the layoutIfNeeded method
self.view.layoutIfNeeded()
})
关于ios - Swift iOS 9键盘出现时不会向上移动 View ,但可在iOS 10中使用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40733710/