TPKeyboardAvoidingScrollView

TPKeyboardAvoidingScrollView

我在屏幕底部放置了一个UIViewUITextField,它将在出现键盘时向上移动。

在iOS 8之前,我一直在遵循以下方法,并且似乎运行良好。

// When Keyboard appears
- (void)keyboardWillShow:(NSNotification *)notification {

[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:[notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue]];
[UIView setAnimationCurve:[notification.userInfo[UIKeyboardAnimationCurveUserInfoKey]integerValue]];
[UIView setAnimationBeginsFromCurrentState:YES];

// Frame Update
CGRect frame = self.bottomView.frame;
frame.origin.y = self.view.frame.size.height - 266.0f;
self.bottomView.frame = frame;

[UIView commitAnimations];
}

// When keyboard disappears
- (void) keyboardHides : (NSNotification *) notification {

[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:[notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue]];
[UIView setAnimationCurve:[notification.userInfo[UIKeyboardAnimationCurveUserInfoKey] integerValue]];
[UIView setAnimationBeginsFromCurrentState:YES];

// Frame update
CGRect frame = self.bottomView.frame;
frame.origin.y = self.view.frame.size.height - self.bottomView.frame.size.height;
self.bottomView.frame = frame;

[UIView commitAnimations];
}

但是上面的代码似乎在iOS 8中不起作用,因为键盘将其背后的UIView挡住了。

经过一番研究,我找到了一个almost-similar答案。但是这里整个UIView被推高了,而我想实现的只是移动bottom UIView

最佳答案

https://github.com/michaeltyson/TPKeyboardAvoiding获取TPKeyboardAvoidingScrollView

如下使用它。

将TPKeyboardAvoidingScrollView.m和TPKeyboardAvoidingScrollView.h源文件拖放到您的项目中,将UIScrollView弹出到视图控制器的xib中,将滚动视图的类设置为TPKeyboardAvoidingScrollView,然后将所有控件放入该滚动视图中。您也可以以编程方式创建它,而无需使用xib-只需将TPKeyboardAvoidingScrollView用作顶层视图即可。

若要禁用自动的“下一步”按钮功能,请将UITextField的返回键类型更改为UIReturnKeyDefault以外的任何值。

07-27 17:28