1.在viewDidload中
[[NSNotificationCenterdefaultCenter]addObserver:selfselector:@selector(keyboardWillShow:)name:UIKeyboardWillShowNotificationobject:nil];
[[NSNotificationCenterdefaultCenter]addObserver:selfselector:@selector(keyboardwillHide:)name:UIKeyboardWillHideNotificationobject:nil];
2.
#pragma mark - Keyboard
- (void)keyboardWillShow:(NSNotification *)notify
{
if (_scrollView.contentOffset.y > 30) {
return;
}
//键盘出现时,view上移
[UIViewbeginAnimations:nilcontext:NULL];
[UIViewsetAnimationDuration:0.3];
[UIViewsetAnimationDelegate:self];
_scrollView.frame =CGRectMake(0, -80,320,_scrollView.height);//130就是上移的距离
[UIViewcommitAnimations];
}
- (void)keyboardwillHide:(NSNotification *)notify
{
//键盘消失后,view还原
[UIViewbeginAnimations:nilcontext:NULL];
[UIViewsetAnimationDuration:0.3];
[UIViewsetAnimationDelegate:self];
_scrollView.frame =CGRectMake(0,0,320,_scrollView.height);
[UIViewcommitAnimations];
}
//如果需要处理中文时
#pragma mark - 切换为中文
- (void)keyboardWillShow:(NSNotification *)notification
{
static CGFloat normalKeyboardHeight = 216.0f;
NSDictionary *info = [notification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
//252 - 216
CGFloat distanceToMove = kbSize.height - normalKeyboardHeight;
//自适应代码
CGRect rect = inputBGView.frame;
inputBGView.frame = CGRectMake(0, rect.origin.y + distanceToMove, rect.size.width, 30);
}