您的位置:首页 > 移动开发 > IOS开发

iOS - 获取系统键盘所在View

2017-01-11 15:55 1131 查看

做自定义键盘可能需要用到这个,也有用更高级别的Window盖住键盘,这个在键盘view上用自定义键盘盖住原键盘,但是记得不需要的时候就把自定义键盘移除,否则在哪都是自定义键盘

代码是从Stack Overflow找到的,好用

- (UIView *)keyboardView
{
UIWindow* tempWindow;

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

NSLog(@"windows %ld", [[[UIApplication sharedApplication]windows]count]);

//Check each window in our application
for(int c = 0; c < [[[UIApplication sharedApplication] windows] count]; c ++)
{
//Get a reference of the current window
tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:c];

//Get a reference of the current view
for(int i = 0; i < [tempWindow.subviews count]; i++)
{
keyboard = [tempWindow.subviews objectAtIndex:i];
NSLog(@"view: %@, on index: %d, class: %@", [keyboard description], i, [[tempWindow.subviews objectAtIndex:i] class]);
if([[keyboard description] hasPrefix:@"(lessThen)UIKeyboard"] == YES)
{
//If we get to this point, then our UIView "keyboard" is referencing our keyboard.
return keyboard;
}
}

for(UIView* potentialKeyboard in tempWindow.subviews)
// if the real keyboard-view is found, remember it.
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) {
if([[potentialKeyboard description] hasPrefix:@"<UILayoutContainerView"] == YES)
keyboard = potentialKeyboard;
}
else if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.2) {
if([[potentialKeyboard description] hasPrefix:@"<UIPeripheralHost"] == YES)
keyboard = potentialKeyboard;
}
else {
if([[potentialKeyboard description] hasPrefix:@"<UIKeyboard"] == YES)
keyboard = potentialKeyboard;
}
}

return keyboard;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息