我正在我的 viewController 中实现一个 textView。这个 TextView 覆盖了整个屏幕,因为我计划让这个 View 供用户写下他们的笔记。但是,当用户触摸 TextView 并弹出键盘时似乎存在问题。
问题是,一旦触摸 TextView ,键盘就会显示一半的屏幕,并且编辑文本的开头会隐藏在键盘后面。我尝试输入一些内容,但根本没有看到文本,因为编辑文本位于键盘后面。有办法解决这个问题吗?
最佳答案
在实现文件中编写 UITextView 的委托(delegate)方法,并将 UITextView 的委托(delegate)设置为 self
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
CGRect rect = txtMessage.frame;
rect.size.height = 91;// you can set y position according to your convinience
txtMessage.frame = rect;
NSLog(@"texView frame is %@",NSStringFromCGRect(textView.frame));
return YES;
}
- (BOOL)textViewShouldEndEditing:(UITextView *)textView{
return YES;
}
- (void)textViewDidEndEditing:(UITextView *)textView{
CGRect rect = txtMessage.frame;
rect.size.height = 276; // set back orignal positions
txtMessage.frame = rect;
NSLog(@"EndTextView frame is %@",NSStringFromCGRect(textView.frame));
}
关于iOS 键盘阻碍了 TextView 中的编辑文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14253487/