键盘显示时,iOS 8 UITextView不会滑动

时间:2014-10-20 16:55:55

标签: keyboard ios8 uitextview

为了避免键盘隐藏文本,我使用以下代码,这在以前版本的iOS中运行得很好。 iOS 8没有任何反应。

- (BOOL)textViewShouldBeginEditing:(UITextView*)textView {        
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.5];

    CGRect frame = self.view.bounds;
    frame.size.height = frame.size.height - [Application keyboardHeight];
    noteView.frame = frame;
    [UIView commitAnimations];

    return YES;
}


- (void)textViewDidEndEditing:(UITextView *)textView {
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.1];

    noteView.frame = self.view.bounds;
    [UIView commitAnimations];
}

也许我错过了一些愚蠢的事情,但我不明白发生了什么。

2 个答案:

答案 0 :(得分:1)

我以不同的方式处理它,它似乎在iOS8中运行良好。此示例将看到UITextView占用可用空间

- (void) viewDidLoad
{
    //Do other stuff

    // listen for keyboard
    [[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 {

    CGSize keyboardSize = [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
    UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
    CGSize size = self.view.frame.size;

    if (orientation == UIDeviceOrientationPortrait || orientation == UIDeviceOrientationPortraitUpsideDown ) 
        textView.frame = CGRectMake(0, 0, size.width , size.height - keyboardSize.height + 50) ;
    else    
        textView.frame = CGRectMake(0, 0, size.width , size.height - keyboardSize.width + 50) ;
}

- (void) keyboardWillHide:(NSNotification *) notification {
    #pragma unused(notification) 

    textView.frame = CGRectMake(0, 0, self.view.frame.size.width , self.view.frame.size.height) ;
}

不确定这是否是答案,但作为答案发布,因此代码已格式化。希望它有所帮助。

答案 1 :(得分:0)

我不确定你的[应用程序keyboardHeight]返回什么,但我遇到了类似的问题,我发现了问题所在。使用@RobCroll使用的相同通知方法时,您必须在iOS7及更早版本中将keyboardSize.width用于横向方向。但是在iOS8中,Apple最终修复了这个bug,你也必须使用keyboardSize.height来实现景观!也许您的[Application keyboardHeight]仍然使用keyboardSize.width?