如何防止键盘覆盖 UITextView 中的文本?

Pre*_*nda 5 ios swift

我在 ViewController 中有一个覆盖整个屏幕的 UITextView,但是当我运行应用程序并在其中输入内容时,如果我向下输入键盘,那么它不会向上滚动。只有当我一直向下输入到屏幕最底部时,它才会向上滚动,但如果我输入超过键盘所在的位置,则 TextView 将不会滚动,并且我输入的任何内容都会被隐藏。我该如何解决这个问题?

小智 0

您可以更改文本视图的框架,使其不在键盘下方。

您可以使用此代码对显示的键盘做出反应并获取键盘的大小。

[[NSNotificationCenter defaultCenter] addObserver:self
                                 selector:@selector(keyboardWasShown:)
                                     name:UIKeyboardDidShowNotification
                                   object:nil];

- (void)keyboardWasShown:(NSNotification *)notification
{

 CGSize keyboardSize = [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;


int height = MIN(keyboardSize.height,keyboardSize.width);
int width = MAX(keyboardSize.height,keyboardSize.width);

//Place code here to resize textview

}
Run Code Online (Sandbox Code Playgroud)