iOS键盘位置和方向

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

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

我的问题是:

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

  2. 有没有办法检测键盘显示在屏幕外,而不pipe设备types和屏幕大小? 我想这是可行的跟踪UIKeyboardWillChangeFrameNotificationUIKeyboardWillShowNotification

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

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

2.)你可以做的是,在通知UIKeyboardWillShowNotificationUIKeyboardWillChangeFrameNotification方法触发的通知UIKeyboardWillShowNotification ,循环通过Windows子视图来定位键盘。 在我的一个应用程序中,我需要添加一个子视图到键盘。 对于你的情况,你可以通过这样做的框架:

 //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.)不完全确定这是可能的,但与我提供的代码给你。 keyboard现在被转换成一个'UIView',你可以应用你自己的转换。

这可能不是most优雅的解决scheme,但对我的情况非常有效。

希望这可以帮助 !

从文档:

使用“键盘通知用户信息键”中描述的键从userInfo字典中获取键盘的位置和大小。

用于从键盘通知的用户信息字典获取值的键:

 NSString * const UIKeyboardFrameBeginUserInfoKey; NSString * const UIKeyboardFrameEndUserInfoKey; NSString * const UIKeyboardAnimationDurationUserInfoKey; NSString * const UIKeyboardAnimationCurveUserInfoKey;