当uiviewcontroller 上有键盘时,任何人都能给我一个快速编程的例子吗
当键盘存在时,如何使uitextfield向上移动
使用此代码
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
self.view.endEditing(true)
}
func textFieldDidBeginEditing(textField: UITextField) {
textField.keyboardType = UIKeyboardType.EmailAddress
self.animateViewMoving(true, moveValue: 50)
}
func textFieldDidEndEditing(textField: UITextField) {
self.animateViewMoving(false, moveValue: 50)
}
func textFieldShouldReturn(textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}
func animateViewMoving (up:Bool, moveValue :CGFloat){
var movementDuration:NSTimeInterval = 0.3
var movement:CGFloat = ( up ? -moveValue : moveValue)
UIView.beginAnimations( "animateView", context: nil)
UIView.setAnimationBeginsFromCurrentState(true)
UIView.setAnimationDuration(movementDuration )
self.view.frame = CGRectOffset(self.view.frame, 0, movement)
UIView.commitAnimations()
}
使用TPKeyboardAvoiding。只需将Scrollview的类设置为TPKeyboardAvoidingScrollView&导入代码中的文件。使用以下链接https://github.com/michaeltyson/TPKeyboardAvoiding