Dav*_*ers 5 uiscrollview uitextview uikeyboard ios uipopover
我有UIPopover一个UIScrollView包含它里面UITextView的底部.当键盘显示时,弹出窗口会在文本视图开始编辑时调整大小.我想要下面的代码,以确保文本视图可见:
- (void)textViewDidBeginEditing:(UITextView *)textView {
CGRect visRect = textView.frame;
[self.scrollView scrollRectToVisible:visRect animated:NO];
}
Run Code Online (Sandbox Code Playgroud)
问题是代码不会使整个文本视图可见.相反,只显示光标底部的文本视图,如下所示:

如何显示整个文本视图/将滚动视图滚动到底部?我试过这个:
CGPoint bottomOffset = CGPointMake(0, self.scrollView.contentSize.height - self.scrollView.bounds.size.height);
[self.scrollView setContentOffset:bottomOffset animated:YES];
Run Code Online (Sandbox Code Playgroud)
正如在这个答案中解释但没有任何作用.
此外,我的滚动视图滚动到键盘移动到位后显示的位置.理想情况下,我希望在键盘移动之前或期间进行滚动.
任何帮助都会很棒.
我找到了解决方案:
- (void)keyboardDidShow:(NSNotification *)notification {
NSLog(@"Notification: %s", __PRETTY_FUNCTION__ );
//
CGFloat textviewBottom = CGRectGetMaxY(self.commentsTextView.frame);
CGRect belowTextViewRect = CGRectMake(0, textviewBottom, 350.f, self.scrollView.contentSize.height - textviewBottom);
// NB! This works ONLY: 1) keyboardDidShow 2) Non-animated;
// Does NOT work: 1) animated, 2) keyboardWillShow, 3) textViewDidBeginEditing
[self.scrollView scrollRectToVisible:belowTextViewRect animated:NO];
}
Run Code Online (Sandbox Code Playgroud)