我有一个调出键盘的文本字段。键盘隐藏文本字段的底部。我已将textField放在滚动视图中,并在视图中编写了以下代码。
- (void)registerForKeyboardNotifications {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasShown:) name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillBeHidden:) name:UIKeyboardWillHideNotification object:nil];
}
- (void)keyboardWasShown:(NSNotification *)aNotification {
// Called when the keyboard is shown
NSDictionary *info = [aNotification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0);
_driverSignupScrollView.contentInset = contentInsets;
_driverSignupScrollView.scrollIndicatorInsets = contentInsets;
// If active field is hidden by keyboard, scroll it so it's visible
CGRect aRect = self.view.frame;
aRect.size.height -= kbSize.height;
CGPoint activeFieldPoint = CGPointMake(_activeField.frame.origin.x, _activeField.frame.origin.y + _activeField.frame.size.height);
if (!CGRectContainsPoint(aRect, activeFieldPoint)) {
[_scrollView scrollRectToVisible:_activeField.frame animated:YES];
}
}
- (void)keyboardWillBeHidden:(NSNotification *)aNotification {
// Called when the keyboard is hidden
UIEdgeInsets contentInsets = UIEdgeInsetsZero;
_scrollView.contentInset = contentInsets;
_scrollView.scrollIndicatorInsets = contentInsets;
}
但控件永远不会进入我的if (!CGRectContainsPoint(aRect, activeFieldPoint))
,因此它不会向上滚动。
正在另一种方法中正确设置_activeField
。
这里有什么我想念的吗?
答案 0 :(得分:1)
查看TPKeyboardAvoiding:https://github.com/michaeltyson/TPKeyboardAvoiding
它使键盘管理非常简单。这就是Apple应该如何设计它的开始。
答案 1 :(得分:0)
你可以把你的textField放在UIView&试着提升你的UIView。