我是iOS SDK的新手,我遇到了一个非常奇怪的问题,关于我正在开发的应用程序的设备键盘位置和方向。问题是,如果在用户多任务或应用程序进入后台时键盘是打开的,那么在用户返回应用程序后,键盘将被替换( UIKeyboardWillChangeFrameNotification被提升),但方向和位置不正确。
有时键盘也会完全脱离屏幕,这是完全不想要的行为。
我的问题是:
UIKeyboardWillShowNotification.UIKeyboardWillChangeFrameNotification,我会在显示键盘之前重置/设置键盘的位置和方向吗?这可能吗?发布于 2012-06-09 02:09:19
1.)键盘是UIWindow,位置取决于应用程序的主窗口。
2.)您可以做的是,在触发通知UIKeyboardWillShowNotification或UIKeyboardWillChangeFrameNotification方法之一时,循环通过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优雅的解决方案,但它适用于我的情况。
希望这能帮上忙!
发布于 2012-10-12 07:47:28
从文件中:
使用“键盘通知用户信息键”中描述的键从userInfo字典中获取键盘的位置和大小。
用于从键盘通知的用户信息字典中获取值的键:
NSString * const UIKeyboardFrameBeginUserInfoKey;
NSString * const UIKeyboardFrameEndUserInfoKey;
NSString * const UIKeyboardAnimationDurationUserInfoKey;
NSString * const UIKeyboardAnimationCurveUserInfoKey;https://stackoverflow.com/questions/10955888
复制相似问题