iOS 8自定义输入视图背景颜色

时间:2014-09-23 16:08:58

标签: background keyboard ios8

我有一个自定义输入视图,我用iOS键盘换出。在iOS 8之前,对于iOS 7,我通过查找类UIKBInputBackdropView(由UIPeripheralHostView包含)的子视图来“触发”键盘背景。然后我能够设置背景视图的alpha以获得清晰的自定义输入视图背景。

使用iOS 8,这不再有效(我意识到它是不受支持的API,这是风险)。通过这里的一些实验和阅读,似乎现在可以在视图层次结构中找到自定义输入视图:

UIInputSetContainerView - > UIInputSetHost

看起来不再有背景视图提供自定义输入视图后面的不透明度。有人能指出我如何摆脱我的自定义输入视图背后的半透明/模糊效果?

2 个答案:

答案 0 :(得分:6)

我在iOS 8上遇到了同样的问题,并且有办法从输入视图中删除背景。

- (void)removeKeyboardBackground
{
    // Locate non-UIWindow.
    UIWindow *keyboardWindow = nil;
    for (UIWindow *testWindow in [[UIApplication sharedApplication] windows]) {
        if (![[testWindow class] isEqual:[UIWindow class]]) {
            keyboardWindow = testWindow;
            break;
        }
    }
    // Locate background.
    for (UIView *possibleFormView in [keyboardWindow subviews]) {
        if ([[possibleFormView description] hasPrefix:@"<UIInputSetContainerView"]) {
            for (UIView* peripheralView in possibleFormView.subviews) {
                if ([[peripheralView description] hasPrefix:@"<UIInputSetHostView"]) {
                    for (UIView* backSubiview in peripheralView.subviews) {

                    if ([[backSubiview description] hasPrefix:@"<UIKBInputBackdropView"]) {
                        [[backSubiview layer] setOpacity:0.0];
                    }
                }
                }

            }
        }
    }
}

答案 1 :(得分:0)

我在后来的iOS版本中使用了可接受答案的变体。看来Apple现在已将UIKBInputBackdropView推到另一个UIView下:

#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:(v) options:NSNumericSearch] != NSOrderedAscending)

- (void)removeKeyboardBackground
{
    NSString *viewPath;

    if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"11.0"))
        viewPath = @"UIRemoteKeyboardWindow/UIInputSetContainerView/UIInputSetHostView/UIView/UIKBInputBackdropView";
    else
        viewPath = @"UIRemoteKeyboardWindow/UIInputSetContainerView/UIInputSetHostView/UIKBInputBackdropView";

    NSArray *appWindows = [NSMutableArray arrayWithArray:[[UIApplication sharedApplication] windows]];
    NSArray *viewsFound = [self viewsFromViews:appWindows AtPath:[viewPath componentsSeparatedByString:@"/"]];

    for (UIView *background in viewsFound)
        background.layer.opacity = 0.0;
}

- (NSArray<__kindof UIView *> *)viewsFromViews:(NSArray<__kindof UIView *> *)views AtPath:(NSArray<NSString *> *)path
{
    NSMutableArray *viewsFound = [NSMutableArray array];

    if (views != nil && path != nil && [views count] > 0 && [path count] > 0)
    {
        NSString *prefix = [@"<" stringByAppendingString:[path firstObject]];
        NSArray *pathRemaining = [path count] <= 1 ? nil : [path subarrayWithRange:NSMakeRange(1, [path count] - 1)];

        for (UIView *view in views)
            if ([[view description] hasPrefix:prefix]) {
                if (pathRemaining == nil)
                    [viewsFound addObject:view];
                else
                    [viewsFound addObjectsFromArray:[self viewsFromViews:[view subviews] AtPath:pathRemaining]];
            }
    }

    return viewsFound;
}