在 iPhone 小键盘 iOS9 上方添加自定义按钮

Adding custom button above iPhone numpad iOS9

我们都知道如何在 iPhone 上的普通数字键盘上方添加自定义按钮(通常是完成)。与此相关的问题很少:

how to get keyboard location in ios 8 & add DONE button on numberPad

Can't find keyplane that supports type 4 for keyboard iPhone-Portrait-NumberPad; using 3876877096_Portrait_iPhone-Simple-Pad_Default

它们在 iOS9 之前工作正常。 iOS9 打破了现有的键盘视图层次结构和上述解决方案不再有效的问题。我花了几个小时试图找出差异,并决定在此处 post 它可能对其他人有用。

与适用于 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];
    }

}