键盘上方的自定义视图不适用于ios9

时间:2016-02-01 16:21:16

标签: ios custom-keyboard ios9.2

在键盘顶部添加了一个由按钮组成的自定义视图。按钮正在正确显示,但点击按钮时,按下键盘的基础键而不是按钮操作。

UIWindow* tempWindow = [UIApplication sharedApplication].windows.lastObject;
for (UIView *keyboard in [tempWindow subviews]) {
    if ([[keyboard description] hasPrefix : @"<UIInputSetContainerView"]) {
    for(int i = 0 ; i < [keyboard.subviews count] ; i++)
       {
        UIView* hostkeyboard = [keyboard.subviews objectAtIndex:i];
        if([[hostkeyboard description] hasPrefix:@"<UIInputSetHost"] == YES){
            [hostkeyboard addSubview:extraRow];
            [hostkeyboard bringSubviewToFront:extraRow];
           }
       }
    }
}

extraRow是由按钮组成的UIView。

有什么遗漏吗?

2 个答案:

答案 0 :(得分:0)

您可以将自定义视图作为inputAccessoryView添加到键盘。然后它只是为按钮分配了你想要点击的正确目标

- (void)textFieldDidBeginEditing:(UITextField *)textField {

    UIView  * theView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 200)];
    [theView setBackgroundColor: [UIColor greyColor]];

    UITextField * txtTest = [[UITextField alloc] initWithFrame:CGRectMake(0, 5, theView.frame.size.width, 60)];
    UIButton * btn = [[UIButton alloc] initWithFrame:CGRectMake(20, 65, 100, 30)];

    [btn setTitle:@"Click my Button" forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(alert:) forControlEvents:UIControlEventTouchUpInside];

    // Just put this to see the items or customize the color
    [txtTest setBackgroundColor:[UIColor whiteColor]];
    [btn setBackgroundColor:[UIColor blueColor]];

    // Need this to add it to the view
    [theView addSubview:txtTest];
    [theView addSubview:btn];

    textField.inputAccessoryView = theView;
}

答案 1 :(得分:0)

您可能想要考虑的一件事是将extraRow的exclusiveTouch属性设置为YES。作为免责声明,我自己没有试过这个例子,但我认为你遇到的问题是由于视图传递了触摸事件。