键盘“WillShow”和“WillHide”与轮换 [英] Keyboard "WillShow" and "WillHide" vs. Rotation

查看:124
本文介绍了键盘“WillShow”和“WillHide”与轮换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个视图控制器正在监听UIKeyboardWillShowNotification和UIKeyboardWillHideNotification。这些通知的处理程序调整视图的各个部分,这是标准过程。

I've got a view controller listening for both UIKeyboardWillShowNotification and UIKeyboardWillHideNotification. The handlers for these notifications adjust various parts of the view, which is standard procedure.

以下代码用于从屏幕坐标转换键盘rect:

The following code is used to convert the keyboard rect from screen coordinates:

CGRect keyboardBounds = [self.view convertRect:[keyboardBoundsValue CGRectValue] fromView:nil];

再次,标准程序。不幸的是,存在这种转换失败的严重情况。看一下当部署键盘时iPhone从纵向旋转到横向时会发生什么:

Again, standard procedure. Unfortunately, there is a critical situation where this conversion fails. Look at what happens when an iPhone is rotated from portrait to landscape while the keyboard is deployed:

1)iOS自动触发 UIKeyboardWillHideNotification ; self.interfaceOrientation报告为 portrait ; keyboardBounds.height 216.0 这是有道理的。为什么?因为通知处理程序在视图切换到横向模式之前有机会清理。

1) iOS automatically fires UIKeyboardWillHideNotification; self.interfaceOrientation is reported as portrait; keyboardBounds.height is 216.0. This makes sense. Why? Because the notification handler is given the chance to "clean up" before the view switches to landscape mode.

2)iOS自动触发 UIKeyboardWillShowNotification ; self.interfaceOrientation报告为 portrait ; keyboardBounds.height 480.0 这没有意义。为什么不?因为通知处理程序会认为键盘的高度是480.0!

2) iOS automatically fires UIKeyboardWillShowNotification; self.interfaceOrientation is reported as portrait; keyboardBounds.height is 480.0. This does NOT make sense. Why not? Because the notification handler is going to do its work thinking that the height of the keyboard is 480.0!

Apple是否将球放在这个上,或者我做错了什么?

Did Apple drop the ball on this one, or am I doing something wrong?

请注意,聆听UIKeyboard 是否 ShowNotification不是有效的解决方案,因为它会显着降低用户体验。为什么?因为在键盘部署动画发生后动画我对视图的更改是...好吧,非常糟糕。

Please note that listening instead for UIKeyboardDidShowNotification is not a valid solution, because it significantly degrades the user experience. Why? Because animating my changes to the view after the keyboard deployment animation occurs is... well, pretty terrible-looking.

有人设法让键盘自动旋转完美部署? Apple似乎完全忽视了混乱的爆发。 >:|

Has anyone managed to get autorotation working perfectly while the keyboard is deployed? It seems like an explosion of chaos that Apple has completely overlooked. >:|

推荐答案

也许有点晚了,但我遇到了同样的问题并为它提供了一个很好的解决方案这可以避免任何类型的工作(当然除非苹果更换东西)

Maybe a bit late, but I've just run into the same issue and have a nice solution for it that avoids any kind of work arounds (unless of course apple change things)

基本上,当通知中心为调用你的方法时,UIKeyboardWillShowNotification (或任何其他通知),它为您提供 UIKeyboardFrameBeginUserInfoKey 的框架位于窗口的上下文中,而不是您的视图。这个问题是,无论设备的方向如何,窗口坐标系都是纵向的,因此你发现宽度和高度都是错误的。

Basically, when the notification center calls your method for UIKeyboardWillShowNotification (or any of the other notifications), the frame that it gives you for UIKeyboardFrameBeginUserInfoKey is in context of the window, NOT your view. The problem with this, is that the windows coordinate system is always in portrait, regardless of the devices orientation, hence you're finding the width and height the wrong way round.

如果您想避免解决问题,只需将矩形转换为视图的坐标系(根据方向进行更改)。为此,请执行以下操作:

If you want to avoid your work around, simply convert the rectangle into the coordinate system of your view (which does change according to the orientation). To do this, do something like the following :

- (void) keyboardWillShow:(NSNotification *)aNotification
{
     CGRect keyboardFrame = [[[aNotification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue];
    CGRect convertedFrame = [self.view convertRect:keyboardFrame fromView:self.view.window];

    ......
    /* Do whatever you want now with the new frame.
     * The width and height will actually be correct now
     */
    ......
}

希望这应该是你所追求的:)

Hopefully this should be what you're after :)

这篇关于键盘“WillShow”和“WillHide”与轮换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆