IOS-swift键盘弹出时,view上滚不遮挡输入框

平时QQ在输入时,其整个view上滚,但实际上直接召唤键盘并不会这样,为了达到这样的效果,我们可以设置一个textfield,并把它的delegate给view,并在view中添加如下代码。即重载animateTextField函数,其中movementDistance为收缩距离,需要根据不同机型进行调整。


func animateTextField(textField: UITextField, up: Bool) {
let movementDistance:CGFloat = -210
let movementDuration: Double = 0.3

var movement:CGFloat = 0
if up {
movement = movementDistance
}
else {
movement = -movementDistance
}
UIView.beginAnimations("animateTextField", context: nil)
UIView.setAnimationBeginsFromCurrentState(true)
UIView.setAnimationDuration(movementDuration)
self.view.frame = CGRectOffset(self.view.frame, 0, movement)
UIView.commitAnimations()
}

func textFieldDidBeginEditing(textField: UITextField) {
self.animateTextField(textField, up:true)
}

func textFieldDidEndEditing(textField: UITextField) {
self.animateTextField(textField, up:false)
}
func textFieldShouldReturn(textField: UITextField) -> Bool // called when 'return' key pressed. return NO to ignore.
{
textField.resignFirstResponder()
return true;
}

发表评论