将UIImageView作为子视图添加到UIKeyboard - userInteractionEnabled?

时间:2012-01-27 16:19:49

标签: objective-c ios ipad keyboard uiimageview

在我的UIKeyboard中添加子视图时遇到了一些问题。我可以成功找到我的键盘并添加一个uiimageview,但遗憾的是图像视图下的按钮仍然可以被触摸。我只是想让它成为一个颜色叠加层,它会隐藏不需要的键并使它们不可触摸:)

这是我的代码,我做错了什么? UIImageView正确显示,但按钮仍然可以点击...... :(

- (UIView *)findKeyboard {

    // Locate non-UIWindow.
    UIWindow *keyboardWindow = nil;
    for (UIWindow *testWindow in [[UIApplication sharedApplication] windows]) {
        if (![[testWindow class] isEqual:[UIWindow class]]) {
            keyboardWindow = testWindow;
            break;
        }
    }

    // Locate UIKeyboard.  
    UIView *foundKeyboard = nil;
    for (UIView __strong *possibleKeyboard in [keyboardWindow subviews]) {

        // iOS 4 sticks the UIKeyboard inside a UIPeripheralHostView.
        if ([[possibleKeyboard description] hasPrefix:@"<UIPeripheralHostView"]) {
            possibleKeyboard = [[possibleKeyboard subviews] objectAtIndex:0];
        }                                                                                

        if ([[possibleKeyboard description] hasPrefix:@"<UIKeyboard"]) {
            foundKeyboard = possibleKeyboard;
            break;
        }
    }
    return foundKeyboard;
}   

- (void)keyboardWillShow:(NSNotification *)note {  

    UIImageView *overlayView;
    UIView *keyboardView = [self findKeyboard];
    UIImage *img = [UIImage imageNamed:@"keyboardOverlay.png"];
    overlayView = [[UIImageView alloc] initWithImage:img];
    [overlayView setUserInteractionEnabled:NO];
    UIView *newView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, overlayView.frame.size.width, overlayView.frame.size.height)];
    newView.userInteractionEnabled = NO;
    newView.multipleTouchEnabled = NO;
    [newView addSubview:overlayView];
    [keyboardView insertSubview:newView atIndex:40];

}

1 个答案:

答案 0 :(得分:2)

禁用用户交互意味着直接传递触摸。启用它,但没有任何手势识别器或操作,它将吞没所有触摸。