从数字键盘上的自定义 'Done' 按钮接收通知在 iOS 8.3 中不起作用

Receiving notifications from custom 'Done' button on numeric keyboard not working in iOS 8.3

我使用其他线程 like this one 中发布的解决方案将自定义 'Done' 按钮添加到数字小键盘。随着 iOS 8.3 的更新,一些事情发生了变化,因为按钮仍然出现但触摸通知永远不会触发。将按钮从键盘视图移动到超级视图破坏了按钮的位置,但证明了控制工作的通知。

它的行为就像自定义按钮在键盘上的透明层后面,无法进行任何触摸。

- (void)addButtonToKeyboard
{
// create custom button
self.doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
self.doneButton.frame = CGRectMake(0, 163+44, 106, 53);
self.doneButton.adjustsImageWhenHighlighted = NO;
[self.doneButton setTag:67123];
[self.doneButton setImage:[UIImage imageNamed:@"doneup1.png"]        forState:UIControlStateNormal];
[self.doneButton setImage:[UIImage imageNamed:@"donedown1.png"] forState:UIControlStateHighlighted];

[self.doneButton addTarget:self action:@selector(doneButton:) forControlEvents:UIControlEventTouchUpInside];

// locate keyboard view
int windowCount = [[[UIApplication sharedApplication] windows] count];
if (windowCount < 2) {
    return;
}

UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1];
UIView* keyboard;

for(int i = 0 ; i < [tempWindow.subviews count] ; i++)
{
    keyboard = [tempWindow.subviews objectAtIndex:i];
    // keyboard found, add the button

    if([[keyboard description] hasPrefix:@"<UIPeripheralHost"] == YES){
        UIButton* searchbtn = (UIButton*)[keyboard viewWithTag:67123];
        if (searchbtn == nil)//to avoid adding again and again as per my requirement (previous and next button on keyboard)
            [keyboard addSubview:self.doneButton];

    }//This code will work on iOS 8.0
    else if([[keyboard description] hasPrefix:@"<UIInputSetContainerView"] == YES){

        for(int i = 0 ; i < [keyboard.subviews count] ; i++)
        {
            UIView* hostkeyboard = [keyboard.subviews objectAtIndex:i];

            if([[hostkeyboard description] hasPrefix:@"<UIInputSetHost"] == YES){
                UIButton* donebtn = (UIButton*)[hostkeyboard viewWithTag:67123];
                if (donebtn == nil)//to avoid adding again and again as per my requirement (previous and next button on keyboard)
                    [hostkeyboard addSubview:self.doneButton];
            }
        }
    }
}
}

我遇到了同样的问题,我通过直接在UITextEffectsWindow上添加按钮解决了这个问题。我还更改了按钮框架。

- (void)addButtonToKeyboar {
// create custom button
self.doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
self.doneButton.frame = CGRectMake(0, [[UIScreen mainScreen] bounds].size.height - 53, [[UIScreen mainScreen] bounds].size.width / 3, 53);
self.doneButton.adjustsImageWhenHighlighted = NO;
[self.doneButton setTag:67123];
[self.doneButton setImage:[UIImage imageNamed:@"doneup1.png"]        forState:UIControlStateNormal];
[self.doneButton setImage:[UIImage imageNamed:@"donedown1.png"] forState:UIControlStateHighlighted];

[self.doneButton addTarget:self action:@selector(doneButton:) forControlEvents:UIControlEventTouchUpInside];

// locate keyboard view
int windowCount = [[[UIApplication sharedApplication] windows] count];
if (windowCount < 2) {
    return;
}

UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1];
UIButton* donebtn = (UIButton*)[tempWindow viewWithTag:67123];
if (donebtn == nil)//to avoid adding again and again as per my requirement (previous and next button on keyboard)
    [tempWindow addSubview:self.doneButton]; }

objectAtIndex:1 可能不会每次都以 window 索引为目标。因此,代码应根据其描述检查 window 是否为 UITextEffectsWindow,如下所示。

if (windowCount < 2) {
    return;

} else {
    periodButton.frame = CGRectMake(0, [[UIScreen mainScreen] bounds].size.height - 53, 106, 53);
    for (UIWindow *window in [[UIApplication sharedApplication] windows]) {
        if ([[window description] hasPrefix:@"<UITextEffectsWindow"]) {
            [window addSubview:periodButton];
        }
    }

}