-1

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

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。

有什么遗漏吗?

4

2 回答 2

0

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

于 2016-03-22T01:47:33.560 回答
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;
}
于 2016-03-22T01:24:49.443 回答