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
優雅的解決方案,但它適用於我的情況。
希望這有助於!
這是非常巧妙的解決辦法尋找鍵盤的位置。但出於某種原因,它似乎並不總能找到鍵盤。我已經在我的應用程序中遍歷了所有的'UIWindow',並且有時我什麼都沒有,即使鍵盤已經啓動。 – Zeenobit
此外,關於您對問題1的回答:如果鍵盤位置僅取決於應用程序的主窗口,那麼在什麼情況下它會在主窗口外面彈出? – Zeenobit