0

很长一段时间以来,我一直在为这个键盘和滚动视图问题苦苦挣扎。我正在尝试制作一个类似于 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];
}
4

1 回答 1

1

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

于 2016-05-12T00:40:15.660 回答