UIKeyBoard调整方向更改为横向

时间:2010-03-19 02:40:19

标签: objective-c uiview uitoolbar uisplitviewcontroller uikeyboard

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

当键盘是hiiden我这样做

toolbarFrame.origin.y += keyBoardSize.height;

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

6 个答案:

答案 0 :(得分:10)

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

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

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

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

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

答案 1 :(得分: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];
}

答案 2 :(得分:4)

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

答案 3 :(得分: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;
}

答案 4 :(得分:0)

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

我发现最好的解决方案是注意键盘框架的“y”属性被指定在屏幕的底部(暗示虚拟键盘实际存在,就在屏幕外)。 / p>

答案 5 :(得分:0)

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

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

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