我有一个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文档,但是对于定位问题没有太多帮助。

最佳答案

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

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

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

关于iphone - 键盘,方向和UITextView,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/5808092/

10-11 19:46
查看更多