UITextView's inputView on iOS 7

前端 未结 3 1561
栀梦
栀梦 2021-02-06 11:14

I\'m trying to create a custom keyboard for a UITextField, the background of this inputView should be transparent, I have set the background color in the view\'s xib file to \"c

3条回答
  •  情歌与酒
    2021-02-06 11:36

    This will set the backdrops opacity to zero when displaying your custom keyboard and reset it back to 1 when the normal keyboard is shown.

    + (void)updateKeyboardBackground {
        UIView *peripheralHostView = [[[[[UIApplication sharedApplication] windows] lastObject] subviews] lastObject];
    
        UIView *backdropView;
        CustomKeyboard *customKeyboard;
    
        if ([peripheralHostView isKindOfClass:NSClassFromString(@"UIPeripheralHostView")]) {
            for (UIView *view in [peripheralHostView subviews]) {
                if ([view isKindOfClass:[CustomKeyboard class]]) {
                    customKeyboard = (CustomKeyboard *)view;
                } else if ([view isKindOfClass:NSClassFromString(@"UIKBInputBackdropView")]) {
                    backdropView = view;
                }
            }
        }
    
        if (customKeyboard && backdropView) {
            [[backdropView layer] setOpacity:0];
        } else if (backdropView) {
            [[backdropView layer] setOpacity:1];
        }
    }
    
    + (void)keyboardWillShow {
        [self performSelector:@selector(updateKeyboardBackground) withObject:nil afterDelay:0];
    }
    
    + (void)load {
        NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
        [nc addObserver:self selector:@selector(keyboardWillShow) name:UIKeyboardWillShowNotification object:nil];
    }
    

提交回复
热议问题