键盘、方向和 UITextView

发布于 2024-11-04 00:22:18 字数 845 浏览 0 评论 0 原文

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

这在纵向模式下工作得很好,但在横向模式下视图完全消失。有没有更优雅的解决方案来处理这个问题?我阅读了苹果键盘管理文档,但它对于方向问题没有提供太多帮助。

I've got a UITextview that covers the entire screen. To compensate for the keyboard I added this handler:

- (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)];
}

This works dandy in portrait mode, but the view completely disapears in landscape mode. Is there a more elegant solution for dealing with this? I read apples keyboard management documentation, but it doesn't have much to offer for orientation issues.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

妞丶爷亲个 2024-11-11 00:22:19

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

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

我认为这是解决这个问题的唯一方法。不过,我仍然喜欢一个优雅的解决方案:)

So, my unfortunate solution is this

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

Which i suppose is the only way to address this. I'd still love an elegant solution though :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文