iOS 7上的UITextView的inputView

时间:2013-09-24 14:33:03

标签: iphone ios uitextfield custom-controls ios7

我正在尝试为UITextField创建自定义键盘,此inputView的背景应该是透明的,我已将视图的xib文件中的背景颜色设置为“clear color”。它在iOS 6及更早版本上运行良好..但在iOS 7上它不起作用 任何想法我怎么能让它工作?我希望它完全透明

3 个答案:

答案 0 :(得分:5)

这会在显示自定义键盘时将背景不透明度设置为零,并在显示普通键盘时将其重置为1.

+ (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];
}

答案 1 :(得分:0)

我正在追逐相同的问题,因为我有一个数字小键盘,只能在横向模式下填充屏幕的左半部分(并且在iOS7上基本上无法使用,其中模糊效果覆盖整个屏幕宽度)。我还没弄明白如何得到我想要的东西(仅在我的实际inputView后面模糊背景),但我已经想出如何完全禁用模糊:

  1. 定义UIView的自定义子类并在xib文件中指定
  2. 在此类中覆盖willMoveToSuperview:如下

    - (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];
        }
    }
    
  3. 这似乎会影响我拥有的每个自定义inputView(但不是系统键盘)的背景,因此如果您不想要将inputView从superview中删除,则可能需要保存/恢复正常的不透明度值。那。

答案 2 :(得分:0)

iOS 7正在做一些未记录的事情。但是,您可以通过在自定义输入视图中覆盖-willMoveToSuperview来检查视图层次结构并调整相关视图。例如,此代码将使背景透明:

- (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;
            }
        }
    }
}