iOS键盘位置和方向 [英] iOS Keyboard Location and Orientation

查看:208
本文介绍了iOS键盘位置和方向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对iOS SDK比较陌生,而且我正在遇到一个关于我正在处理的应用程序的设备键盘位置和方向的非常奇怪的问题。问题是如果键盘在用户多任务或应用程序进入后台时打开,在用户返回应用程序后,键盘将被移位(使用 UIKeyboardWillChangeFrameNotification 被提升),但方向和位置不正确。

I'm relatively new to iOS SDK, and I'm experiencing a very bizarre issue regarding the device keyboard location and orientation for an app I'm working on. The problem is that if the keyboard is open while the user multi-tasks or the app goes into background, after the user comes back to the app, the keyboard will be be displaced (with UIKeyboardWillChangeFrameNotification being raised), but in an incorrect orientation and location.

有时候键盘也会完全脱离屏幕,这是完全不受欢迎的行为。

Sometimes the keyboard shows up completely off the screen too, which is completely undesired behaviour.

我的问题是:


  1. 键盘的位置和方向是什么依赖?它是如何由iOS控制的?

  1. What is the position and orientation of the keyboard dependant on? How is it controlled by iOS?

有没有办法检测键盘何时在屏幕外显示,无论设备类型和屏幕尺寸如何?我认为可以通过跟踪 UIKeyboardWillChangeFrameNotification UIKeyboardWillShowNotification 来实现。

Is there a way to detect when the keyboard is being displayed off-screen regardless of the device type and screen size? I'm thinking it would be doable by tracking UIKeyboardWillChangeFrameNotification or UIKeyboardWillShowNotification.

在显示键盘之前,如何重置/设置键盘的位置和方向?这甚至可能吗?

How would I reset/set the location and orientation of the keyboard prior to displaying it? Is this even possible?


推荐答案

1。)键盘是UIWindow,该位置取决于应用程序的主窗口。

1.) Keyboard is a UIWindow, the position is dependent on the Application's Main Window.

2。)您可以做的是,在其中一个通知 UIKeyboardWillShowNotification UIKeyboardWillChangeFrameNotification 方法触发,循环浏览Windows子视图以找到键盘。在我的一个应用程序中,我需要在键盘上添加子视图。对于你的情况,你可以通过这样做得到框架:

2.) What you could do is, upon the one of the notifications UIKeyboardWillShowNotification or UIKeyboardWillChangeFrameNotification method firing, loop through the windows subviews to locate the Keyboard. In one of my applications I needed to add a subview to the keyboard. For your case you can get the frame by doing this:

//The UIWindow that contains the keyboard view - It some situations it will be better to actually
//iterate through each window to figure out where the keyboard is, but In my applications case
//I know that the second window has the keyboard so I just reference it directly
UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1];

//Because we cant get access to the UIPeripheral throught the SDK we will just use UIView.
//UIPeripheral is a subclass of UIView anyways
UIView* keyboard;

    //Iterate though each view inside of the selected Window
for(int i = 0; i < [tempWindow.subviews count]; i++)
{
    //Get a reference of the current view
    keyboard = [tempWindow.subviews objectAtIndex:i];

           //Assuming this is for 4.0+, In 3.0 you would use "<UIKeyboard"
           if([[keyboard description] hasPrefix:@"<UIPeripheral"] == YES) {

                  //Keyboard is now a UIView reference to the UIPeripheral we want
                  NSLog(@"Keyboard Frame: %@",NSStringFromCGRect(keyboard.frame));

           }
}

3。)不完全确定这个是可能的,但我提供的代码提供给你。 键盘现在已经转换为'UIView',您可以将自己的转换应用到。

3.) Not completely sure this is possible, but with the supplied code I gave you. keyboard is now casted to a 'UIView', which you can apply your own transforms to.

这可能不会是大多数优雅的解决方案,但它适合我的情况。

This might not be the most elegant solution, but it works well for my case.

希望这有帮助!

这篇关于iOS键盘位置和方向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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