UITextView,在编辑时滚动?

我有一个大UITextView ,几乎全屏。 如果我点击它,键盘popup,我可以编辑文本。 但是,我input的时间越长,最终文本就会沿着键盘后面的视图运行。 我不能再看到我在input什么。

你怎么处理这个? 你是否需要跟踪光标位置并手动滚动视图?

我想你不得不把你的UITextView设置为键盘显示/隐藏。 所以键盘不会超过你的textview。 这里是示例代码。

 - (void)viewDidLoad { [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil]; } - (void)keyboardWillShow:(NSNotification *)notification { [UIView beginAnimations:nil context:nil]; CGRect endRect = [[notification.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue]; CGRect newRect = YOUT_TEXT_VIEW.frame; //Down size your text view newRect.size.height -= endRect.size.height; YOUT_TEXT_VIEW.frame = newRect; [UIView commitAnimations]; } - (void)keyboardWillHide:(NSNotification *)notification { ... // Resize your textview when keyboard is going to hide } 

您需要使用以下代码来根据文本范围向下滚动textview(或根据input说)

 NSRange range = NSMakeRange(textView.text.length - 1, 1); [textView scrollRangeToVisible:range]; 

希望对你有帮助…

TPKeyboardAvoiding是一个优秀的工具,处理所有的滚动,以避免为您的键盘。 /非常/方便,强烈推荐。 请参阅: https : //github.com/michaeltyson/TPKeyboardAvoiding