Note: pop-up keyboard input box, a corresponding view of the shift
#pragma mark - NOtificationCenter
// add pop-up keyboard and the keyboard hidden listeners
- (void)addObservers
{
[[NSNotificationCenterdefaultCenter] addObserver:selfselector:@selector(keyboardWillShow:)name:UIKeyboardWillShowNotificationobject:nil];
[[NSNotificationCenterdefaultCenter] addObserver:selfselector:@selector(keyboardWillHidden:)name:UIKeyboardWillHideNotificationobject:nil];
}
// Remove listeners
- (void)removeObservers
{
[[NSNotificationCenterdefaultCenter] removeObserver:selfname:UIKeyboardWillShowNotificationobject:nil];
[[NSNotificationCenterdefaultCenter] removeObserver:selfname:UIKeyboardWillHideNotificationobject:nil];
}
// keyboard is about to pop
- (void)keyboardWillShow:(NSNotification *)notification
{
lastContentOffset =scrollView.contentOffset;
CGRect keyboardFrame = [[notification.userInfoobjectForKey : UIKeyboardFrameEndUserInfoKey ] CGRectValue ]; // the Notification. userInfo keyboard information
// first responders, (note: acquiring the actual situation)
UITextField *currentTextField = [nameTextFieldisFirstResponder] ?nameTextField : phoneTextField;
// 第一响应者在滚动式图上坐标
CGRect rect = [currentTextField.superviewconvertRect:currentTextField.frametoView:scrollView];
// 第一响应者距离屏幕底部高度,滚动视图高度 - 第一响应者最下面点坐标 + 滚动视图偏移量 + 滚动视图下面视图高度(注意:此处也要根据实际情况来计算)
CGFloat height =CGRectGetHeight(scrollView.frame) -CGRectGetMaxY(rect) + scrollView.contentOffset.y +50;
CGFloat change = height - keyboardFrame.size.height;
if (change <0) // 如果小于0,视图需要上移
{
scrollView.contentOffset =CGPointMake(scrollView.contentOffset.x,scrollView.contentOffset.y - change);
}
}
// 键盘即将隐藏
- (void)keyboardWillHidden:(NSNotification *)notification
{
scrollView.contentOffset =lastContentOffset;
}