键盘,方向和UITextView

时间:2011-04-27 17:26:33

标签: iphone objective-c ios ipad uitextview

我有一个覆盖整个屏幕的UITextview。为了补偿键盘,我添加了这个处理程序:

- (void)keyboardWasShown:(NSNotification*)aNotification{
    // Resize for the stupid keyboard
    NSDictionary* info = [aNotification userInfo];
    CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;

    CGRect rect = _textView.frame;
    rect.size.height -= kbSize.height;
    _textView.frame = rect;

    CGPoint p = [_textView contentOffset];
    [_textView setContentOffset:p animated:NO];
    [_textView scrollRangeToVisible:NSMakeRange([_textView.text length], 0)];
}

这在纵向模式下工作,但视图在横向模式下完全消失。处理这个问题有更优雅的解决方案吗?我阅读了apple keyboard management文档,但它没有提供很多方向问题。

1 个答案:

答案 0 :(得分:3)

所以,我不幸的解决方案就是这个

UIInterfaceOrientation o = [self interfaceOrientation];
if(o == UIInterfaceOrientationPortrait || o == UIInterfaceOrientationPortraitUpsideDown){
    rect.size.height -= kbSize.height;
}else if(o == UIInterfaceOrientationLandscapeLeft || o == UIInterfaceOrientationLandscapeRight){
    rect.size.height -= kbSize.width;
}

我想这是解决这个问题的唯一方法。我仍然喜欢优雅的解决方案:)