2015-09-06 76 views
3

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

+0

你有没有试图隐藏键盘,只要你不打字? –

+0

@BrunoRecillas是的,但我希望它在用户打字的时候,他们实际上可以在没有键盘的情况下看到他们正在打字的内容。 – PretentiousPanda

+0

好吧。您是否在StoryBoard中为该特定的textView启用了滚动功能? –

回答

0

您可以更改textview的框架,使其不在键盘下方。

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

[[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 

} 
0

你可以试试这个

这些是用于文本框的委托方法。

请确保您在.h文件中包含了<UITextFieldDelegate>

- (void)textFieldDidBeginEditing:(UITextField *)textField 
{ 
    [self animateTextField: textField up: YES]; 
} 


- (void)textFieldDidEndEditing:(UITextField *)textField 
{ 
    [self animateTextField: textField up: NO]; 
} 

- (void) animateTextField: (UITextField*) textField up: (BOOL) up 
{ 
    const int movementDistance = 80; // tweak as needed 
    const float movementDuration = 0.3f; // tweak as needed 

    int movement = (up ? -movementDistance : movementDistance); 

    [UIView beginAnimations: @"anim" context: nil]; 
    [UIView setAnimationBeginsFromCurrentState: YES]; 
    [UIView setAnimationDuration: movementDuration]; 
    self.view.frame = CGRectOffset(self.view.frame, 0, movement); 
    [UIView commitAnimations]; 
} 
0

IQKeyboardManager是为了创造这个确切的问题而建造的。它只需要一行代码:

IQKeyboardManager.sharedManager().enable = true

我会强烈建议使用它,而不是开发自己的解决方案(虽然是很好的做法,但非常耗费时间)。