我一直在努力解决这个键盘和scrollview问题。我正在尝试建立一个类似于What'sApp和iMessage的聊天室。我有UITabBar作为根视图控制器。对于聊天室视图,我在底部有一个包含UITextView和UIButton的工具栏,问题是当键盘出现时它将内容视图推出屏幕,我看不到大约1/5的顶部内容视图。我尝试使用这些数字,仍然无法让它正常工作。任何帮助将不胜感激。
- (void)keyboardWasShown:(NSNotification *) aNotification {
NSDictionary *info = [aNotification userInfo];
CGSize keyboardSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
// the hardcoded 49 is the height of the UITabBar at the bottom below the input toolbar
UIEdgeInsets contentInsets = UIEdgeInsetsMake((-keyboardSize.height+49), 0.0, (keyboardSize.height-49), 0.0);
self.scrollView.contentInset = contentInsets;
self.scrollView.scrollIndicatorInsets = contentInsets;
// If active text field is hidden by keyboard, scroll it so it's visible
// Your app might not need or want this behavior.
// CGRect aaRect = self.view.frame;
// aaRect.size.height -= keyboardSize.height;
// if (!CGRectContainsPoint(aaRect, self.activeTextView.frame.origin)) {
// [self.scrollView scrollRectToVisible:self.activeTextView.frame animated:NO];
// }
CGPoint scrollPoint = CGPointMake(0, self.scrollView.contentInset.bottom);
[self.scrollView setContentOffset:scrollPoint animated:true];
[self.view addGestureRecognizer:self.tapRecognizer];
}
- (void)keyboardWillBeHidden:(NSNotification *) aNotification {
UIEdgeInsets contentInsets = UIEdgeInsetsZero;
self.scrollView.contentInset = contentInsets;
self.scrollView.scrollIndicatorInsets = contentInsets;
[self.view removeGestureRecognizer:self.tapRecognizer];
}
答案 0 :(得分:1)
很久以前我遇到了同样的问题。我的解决方案是听键盘框确实改变通知(因为不同的键盘有不同的框架)。而且我认为调整滚动视图的框架而不是内容偏移更容易。