2011-04-27 81 views
2

我有一个覆盖整个屏幕的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)]; 
} 

这工作在纵向模式花花公子,但鉴于在横向模式完全在前看不见。有处理这个更优雅的解决方案吗?我阅读了苹果keyboard management文档,但它对于定位问题没有多少帮助。

回答

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; 
} 

我估计是解决这一问题的唯一途径。我仍然喜欢优雅的解决方案:)