键盘将UIScrollView内容推出屏幕



我已经为这个键盘和滚动视图问题挣扎了很长一段时间。我正在尝试创建一个类似What's App和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];
}

我很久以前就遇到了同样的问题。我的解决方案是听键盘边框做了更改通知(因为不同的键盘有不同的边框)。我认为调整滚动视图的边框比调整内容偏移更容易。

最新更新