【问题标题】:UIKeyBoard resize on orientation change to landscapeUIKeyBoard 在方向更改为横向时调整大小
【发布时间】:2010-03-19 02:40:18
【问题描述】:

这是一个非常菜鸟的问题。我在底部有一个 UIToolBar,它应该在显示 UIKeyBoard 时随键盘动画地上下移动。我在 UIKeyBoard Notifications 的帮助下完成了这项工作。我们正在谈论的视图启用了拆分视图。当设备方向是横向时,两个视图都显示为列[希望有意义]。

当显示键盘时,我会这样做

CGSize keyBoardSize = [[notificationInfo objectForKey:UIKeyboardFrameEndUserInfoKey]CGRectValue].size;

CGRect toolbarFrame= [BottomToolBar frame];
toolbarFrame.origin.y -= keyBoardSize.height;    
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.3];
BottomToolBar .frame = viewFrame;
[UIView commitAnimations];

当键盘隐藏时我会这样做

toolbarFrame.origin.y += keyBoardSize.height;

我的问题是当设备方向变为横向时,当键盘可见时,底部工具栏消失了。我看到它迅速上升。我不知道如何解决这个问题。有人可以帮忙吗?另外,有没有办法不让键盘跨越拆分视图中的两个视图?

【问题讨论】:

    标签: objective-c uiview uitoolbar uisplitviewcontroller uikeyboard


    【解决方案1】:

    我也有这个问题,我能想到的就是关闭键盘并重新显示它(辞职然后再次成为第一响应者)。但这似乎很不令人满意。

    还请注意,您应该将矩形从屏幕坐标转换为视图坐标。 (屏幕坐标不旋转。)

    CGRect keyboardRect = [[notificationInfo objectForKey:UIKeyboardFrameEndUserInfoKey]CGRectValue];
    keyboardRect = [[BottomToolBar superview] convertRect:keyboardRect fromView:nil];
    

    更新:您必须注册 UIKeyboardWillShowNotification,然后界面旋转时将调用您的操作:)

    另见: https://devforums.apple.com/message/181482#181482

    【讨论】:

    • 如果您试图将工具栏保持在键盘框架上方并且显示通话状态栏,则此转换是必不可少的。您可以在模拟器中使用键盘命令 CMD + Y 进行测试
    【解决方案2】:
    - (void)viewDidLoad {
        [super viewDidLoad];
    
        [[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 *)aNotification 
    {
        CGRect keyboardBounds;
        [[aNotification.userInfo valueForKey:UIKeyboardFrameEndUserInfoKey] getValue: &keyboardBounds];
    
        CGFloat keyboardHeight;
        switch ([UIApplication sharedApplication].statusBarOrientation) {
            case UIInterfaceOrientationPortrait:
            case UIInterfaceOrientationPortraitUpsideDown:
                keyboardHeight = keyboardBounds.size.height;
                break;
            case UIInterfaceOrientationLandscapeLeft:
            case UIInterfaceOrientationLandscapeRight:
                keyboardHeight = keyboardBounds.size.width;
                break;
        }
    
        NSTimeInterval animationDuration = [[[aNotification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    
        CGRect rect = table.frame;
        rect.size.height -= keyboardHeight;
        [UIView beginAnimations:@"ResizeForKeyboardShow" context:nil];
        [UIView setAnimationDuration:animationDuration];
        table.frame = rect;
        [UIView commitAnimations];
    }
    
    - (void)keyboardWillHide:(NSNotification *)aNotification
    {
        CGRect keyboardBounds;
        [[aNotification.userInfo valueForKey:UIKeyboardFrameEndUserInfoKey] getValue: &keyboardBounds];
    
        CGFloat keyboardHeight;
        switch ([UIApplication sharedApplication].statusBarOrientation) {
            case UIInterfaceOrientationPortrait:
            case UIInterfaceOrientationPortraitUpsideDown:
                keyboardHeight = keyboardBounds.size.height;
                break;
            case UIInterfaceOrientationLandscapeLeft:
            case UIInterfaceOrientationLandscapeRight:
                keyboardHeight = keyboardBounds.size.width;
                break;
        }
    
        NSTimeInterval animationDuration = [[[aNotification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    
        CGRect rectTable = table.frame;
        rectTable.size.height += keyboardHeight;
        [UIView beginAnimations:@"ResizeForKeyboard" context:nil];
        [UIView setAnimationDuration:animationDuration];
        table.frame = rectTable;
        [UIView commitAnimations];
    }
    

    【讨论】:

    • 别忘了 [[NSNotificationCenter defaultCenter] removeObserver:self];关于 viewDidUnload 和 dealloc
    • 仅供参考:您可以使用方便的UIDeviceOrientationIsPortrait 宏来代替那个大的switchcase 怪物。 :)
    【解决方案3】:

    UITextFields 有一个 inputAccessoryView 属性,允许在 UIKeyboard 上方添加一个 UIToolBar。

    【讨论】:

    • 是的,但是当键盘隐藏时,OP 希望工具栏在屏幕底部可见。
    • 没问题,只要把底部的工具栏设置为inputAccessoryView就大功告成了。
    【解决方案4】:
    - (void)viewDidLoad { // Or somewhere else
        [super viewDidLoad];
    
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(kbDidChange:) UIKeyboardDidChangeFrameNotification object:nil];
    }
    
    - (void)kbDidChange:(NSNotification *)notification {
        NSDictionary* keyboardInfo = [notification userInfo];
        CGRect keyboardFrame = [[keyboardInfo valueForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
        keyboardFrame = [[messageBar superview] convertRect:keyboardFrame fromView:nil];
    
        CGRect ol = messageBar.frame; // messageBar is your UIToolbar..
        ol.origin.y = keyboardFrame.origin.y-44;
        messageBar.frame = ol;
    }
    

    【讨论】:

    • 您是否可以考虑解释一下这段代码,以及为什么它回答了手头的问题?
    【解决方案5】:

    这些答案效果很好,除了外部键盘。当存在硬件键盘时,消息中传递的键盘框架的“高度”属性看起来仍然与虚拟键盘相同,这意味着此代码从视图框架中减去高度现有的键盘(创造一个尴尬的空间)。

    我发现的最佳解决方案是注意键盘框架的“y”属性被指定在屏幕底部(这意味着虚拟键盘实际上是存在的,就在屏幕之外)。

    【讨论】:

      【解决方案6】:

      解决方案是为基于块的动画设置选项UIViewAnimationOptionBeginFromCurrentState,或者

      [UIView beginAnimations:nil context:nil];
      [UIView setAnimationDuration:[notificationInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue]];
      [UIView setAnimationBeginsFromCurrentState:YES];
      BottomToolBar.frame = viewFrame;
      [UIView commitAnimations];
      

      在你的情况下。这样,当从纵向更改为横向时,视图不会突然移动。

      【讨论】:

        猜你喜欢
        • 2013-10-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-04-13
        相关资源
        最近更新 更多