Swift iOS 9键盘出现不会移动视图,但适用于iOS 10

时间:2016-11-22 04:20:36

标签: ios swift uicollectionview uitextfield

这是我的代码,当键盘出现时将视图向上移动(还要注意此视图有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 **中,当视图首次加载并且我第一次打开键盘时,视图会尝试向上移动但失败。相反,黑色背景似乎暂时向下移动,然后消失:

Black part that appears momentarily then disappears in iOS 9 只有在滚动集合视图之前打开键盘才会发生这种情况。一旦我开始滚动集合视图,错误消失,键盘再次正确向上移动。

此错误也未在iOS 10中显示。

1 个答案:

答案 0 :(得分:0)

试试这个

UIView.animate(withDuration: 1.0, animations: {
    //Do your constraint change part here

}, completion: {bool in

        // Call here the layoutIfNeeded method
        self.view.layoutIfNeeded()
})