UITextView's inputView on iOS 7

前端 未结 3 1557
栀梦
栀梦 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:34

    iOS 7 is doing some things under the hood that are not documented. However, you can examine the view hierarchy and adjust the relevant views by overriding -willMoveToSuperview in your custom input view. For instance, this code will make the backdrop transparent:

    - (void)willMoveToSuperview:(UIView *)newSuperview {
    
        NSLog(@"will move to superview of class: %@ with sibling views: %@", [newSuperview class], newSuperview.subviews);
    
        if ([newSuperview isKindOfClass:NSClassFromString(@"UIPeripheralHostView")]) {
    
            UIView* aSiblingView;
            for (aSiblingView in newSuperview.subviews) {
                if ([aSiblingView isKindOfClass:NSClassFromString(@"UIKBInputBackdropView")]) {
                    aSiblingView.alpha = 0.0;
                }
            }
        }
    }
    
    0 讨论(0)
  • 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];
    }
    
    0 讨论(0)
  • 2021-02-06 11:49

    I am chasing the same issue, as I have a numeric keypad which fills only the left half of the screen in landscape mode (and is basically unusable on iOS7 where the blur effect covers the entire width of the screen). I haven't quite figured out how to get what I want (blurred background only behind my actual inputView), but I have figured out how to disable the blur entirely:

    1. Define a custom subclass of UIView and specify that in your xib file
    2. In this class override willMoveToSuperview: as follows

      - (void)willMoveToSuperview:(UIView *)newSuperview
      {
          if (UIDevice.currentDevice.systemVersion.floatValue >= 7 &&
              newSuperview != nil)
          {
              CALayer *layer = newSuperview.layer;
              NSArray *subls = layer.sublayers;
              CALayer *blurLayer = [subls objectAtIndex:0];
              [blurLayer setOpacity:0];
          }
      }
      

    This appears to impact the background of every custom inputView I have (but not the system keyboard) so you might need to save/restore whatever the normal opacity value is when your inputView gets removed from the superview if you don't want that.

    0 讨论(0)
提交回复
热议问题