我们都知道如何在iPhone上方的普通数字键盘上添加自定义按钮(通常是“完成”)。与此相关的问题很少:
how to get keyboard location in ios 8 & add DONE button on numberPad
他们在iOS9之前工作正常。 iOS9破坏了现有的键盘视图层次结构,上面提到的解决方案不再适用。我花了几个小时试图找出差异,并决定在这里发布,因为它可能对其他人有用。
答案 0 :(得分:1)
与适用于iOS7-8的解决方案的区别如下:
// Note index 2 here! In pre-iOS8 keyboard view panel was under second window after UIWindow, in iOS9 - they put some other views on the place and shifted everything one element down.
UIWindow *tempWindow = [[[UIApplication sharedApplication] windows][2];
UIView *keyboard;
for (int i = 0; i < [tempWindow.subviews count]; i++)
{
keyboard = [tempWindow.subviews objectAtIndex:i];
// keyboard view found; add the custom button to it
if ([[keyboard description] hasPrefix:@"<UIPeripheralHostView"] == YES)
{
[keyboard addSubview:doneButton];
}
}