我有一个大的UITextView,几乎占满了屏幕。当我点击它时,键盘弹出来,我可以编辑文本。然而,当我输入越来越多时,文本最终会在键盘后面超出视图范围,我就看不到我正在输入什么。
你应该如何处理这种情况呢?你需要跟踪光标位置并手动滚动视图吗?
你应该如何处理这种情况呢?你需要跟踪光标位置并手动滚动视图吗?
NSRange range = NSMakeRange(textView.text.length - 1, 1);
[textView scrollRangeToVisible:range];
我猜您需要根据键盘的显示/隐藏来调整UITextView的大小,以便键盘不会覆盖您的文本视图。以下是示例代码。
- (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
}
TPKeyboardAvoiding是一款优秀的工具,它可以帮助你处理所有与键盘避免滚动有关的问题。非常方便且强烈推荐使用。详情请见:https://github.com/michaeltyson/TPKeyboardAvoiding