UIKeyBoard 在方向更改为横向时调整大小

发布于 2024-08-25 07:47:36 字数 710 浏览 10 评论 0原文

这是一个非常菜鸟的问题。我在底部有一个 UIToolBar,当显示 UIKeyBoard 时,它应该随着键盘上下动画移动。我在 UIKeyBoard 通知的帮助下实现了这一点。我们正在讨论的视图启用了分割视图。当设备方向为横向时,两个视图都会显示为列[希望有意义]。

当显示键盘时,我这样做

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;

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

This is very rookie question. I have a UIToolBar at the bottom which is supposed to animatedly move up and down with key board when the UIKeyBoard is displayed. I got that working with the help of UIKeyBoard Notifications. The view we are talking about has split view enabled. When device orientation is landscape, both the views as columns are shown [hope that makes sense].

When key board is shown, i do this

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

when key board is hiiden i do this

toolbarFrame.origin.y += keyBoardSize.height;

My Problem is when device orientation changes to landscape, when the key board is visible the bottom tool bar is gone. I see it move up quickly. I am not sure how to fix this. Can anyone help please? Also, is there a way to NOT make the key board span across both the views in the split view?

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

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

发布评论

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

评论(6

妄想挽回 2024-09-01 07:47:36

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

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

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

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

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

I have this problem too, all I can think of is dismissing the keyboard and reshowing it (resign and then become first responder again). But that seems very unsatisfactory.

Note also you should be converting the rect from screen coordinates to the view's coordinates. (The screen coordinates don't rotate.)

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

UPDATE: You must register for UIKeyboardWillShowNotification, then your action will be called when the interface rotates :)

Also see:
https://devforums.apple.com/message/181482#181482

江湖彼岸 2024-09-01 07:47:36
- (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];
}
- (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];
}
花开柳相依 2024-09-01 07:47:36

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

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

看透却不说透 2024-09-01 07:47:36
- (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;
}
- (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;
}
a√萤火虫的光℡ 2024-09-01 07:47:36

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

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

These answers work well, except for external keyboards. When a hardware keyboard is present, it looks like the "height" property of the keyboard frame passed in the message is still the same as it would be with a virtual keyboard, meaning this code subtracts the height from the view frame for a non-existent keyboard (creating an awkward space).

The best solution I've found is to notice that the "y" property of the keyboard frame is specified to be at the bottom of the screen (implying that the virtual keyboard is actually present, just off the screen).

小清晰的声音 2024-09-01 07:47:36

设置选项 UIViewAnimationOptionBeginFromCurrentState

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

解决方案是为基于块的动画或您的情况 。这样,当方向从纵向更改为横向时,视图不会突然移动。

The solution is to set option UIViewAnimationOptionBeginFromCurrentState for block based animation, or

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

in your case. Then the view will not get a sudden movement when changing orientation from portrait to landscape.

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