3

这是一个非常菜鸟的问题。我在底部有一个 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;

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

4

6 回答 6

10

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

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

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

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

另请参阅: https ://devforums.apple.com/message/181482#181482

于 2010-03-26T05:52:15.320 回答
6
- (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];
}
于 2010-09-20T09:55:29.417 回答
4

UITextFields have an inputAccessoryView property that permits adding a UIToolBar above the UIKeyboard.

于 2011-02-21T16:52:00.173 回答
2
- (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;
}
于 2012-09-02T12:54:22.120 回答
0

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

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

于 2010-12-31T14:53:16.530 回答
0

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

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

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

于 2012-05-31T16:49:15.890 回答