自定义 webview 键盘问题 [英] Custom webview keyboard issues

查看:37
本文介绍了自定义 webview 键盘问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此改编此线程的代码UIWebView 中的UIKeyboardAppearanceTomSwift 的 很棒的答案,我得到了大约 99% 的工作.

So adapting code form this thread UIKeyboardAppearance in UIWebView and TomSwift's awesome answer, I got about 99% of it working.

在 iOS 7 模拟器中,一切似乎都运行良好.但是在 iOS 8 中,当键盘第一次出现时, <> 完成栏是白色的.当我点击或选择另一个输入时,它会更改为我指定的颜色.

In the iOS 7 simulator, everything appears to work just fine. However in iOS 8, when the keyboard first appears, the < > Done bar is white. When I tap or select another input, it changes to my specified color.

我的问题是,我怎样才能防止和/或改变那个白色部分?

My question is, how can I prevent and or change that white portion?

另一个线程中的所有代码都是相同的,除了我在 keyboardWillAppear 中调用的颜色.

All code in the other thread is identical, except for my color which I call like so in the keyboardWillAppear.

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

// Locate UIWebFormView.
for (UIView *possibleFormView in [keyboardWindow subviews]) {
    if ([[possibleFormView description] hasPrefix : @"<UIInputSetContainerView"]) {

        for (UIView* peripheralView in possibleFormView.subviews) {
            peripheralView.backgroundColor = [UIColor colorWithRed:0.271 green:0.271 blue:0.271 alpha:0.75];

            for (UIView* peripheralView_sub in peripheralView.subviews) {
                peripheralView_sub.backgroundColor = [UIColor colorWithRed:0.271 green:0.271 blue:0.271 alpha:0.75];

            }
        }
    }
}

任何帮助将不胜感激.

推荐答案

所以随着 iOS 9+ 的推出,我发现它破坏了上述方法.但是经过一些修补和浏览一些观点,我想出了我在下面已经回答的内容的补充.

So with iOS 9+ out, I found it broke the mentioned methods. But with some tinkering and looking through some views, I came up with an addition to what i've already answered below.

现在我决定放弃自定义颜色的东西,我只挖掘黑色键盘,适合我的应用程序.无论如何,这对我有用.在 9.1 sim 到 7 上测试.也在我的 6+ 上运行 9.0.2.

Now I've decided to ditch the custom color stuff, I'm digging just the black keyboard, suits my app. Anyways, here's what works for me. Tested on 9.1 sim to 7. Also on my 6+ running 9.0.2.

//Keyboard setting
@interface UIWebBrowserView : UIView
@end
@interface UIWebBrowserView (UIWebBrowserView_Additions)
@end
@implementation UIWebBrowserView (UIWebBrowserView_Additions)
- (id)inputAccessoryView {
    return nil;
}

- (UIKeyboardAppearance) keyboardAppearance{

    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    BOOL switchOn = [userDefaults boolForKey:@"darkKeyboard"];

    if (switchOn) {
        return UIKeyboardAppearanceDark;
    }
    else {
        return UIKeyboardAppearanceDefault;
    }
}
@end

@interface UITextInputTraits : UIWebBrowserView
@end
@interface UITextInputTraits (UIWebBrowserView)
@end
@implementation UITextInputTraits (UIWebBrowserView)
- (UIKeyboardAppearance) keyboardAppearance{

    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    BOOL switchOn = [userDefaults boolForKey:@"darkKeyboard"];

    if (switchOn) {
        return UIKeyboardAppearanceDark;
    }
    else {
        return UIKeyboardAppearanceDefault;
    }
}
@end

我真的希望有人觉得这些答案有帮助 :D

I really hope somebody finds these answers helpful :D

更新信息:对完成栏很好奇,这就是这一切的开始.我重新启用它只是为了查看,并发现它已将其更改为黑色.不错的奖励,虽然我已经放弃它以隐藏带有滚动的键盘.

UPDATED INFO: Was curious about the done bar, which is how this all got started. I re-enabled it just to see, and to find out it changed it to black. Nice bonus, although I've ditched it to hide the keyboard with scroll.

更新 12/19/15所以我决定从 UIWebView 过渡到 WKWebView,结果却发现两者显然不同.我已经设法让它再次工作.常规 UIKeyboardAppearanceDark 调用会导致键盘比我喜欢的更透明.所以我根据自己的喜好修改了它.再说一次,可能不是标准的做事方式,但我不在乎,反正它不会是苹果.

UPDATE 12/19/15 So I decided to make my transition from UIWebView to WKWebView, only to find out that obviously things are different between the two. I've managed to get it working again. Regular UIKeyboardAppearanceDark calls causes the keyboard to be more transparent than I like. So I modified it to my liking. Again, probably not the standard way of doing things, but I don't care, it's not going to apple anyways.

所有内容仍在 AppDelegate 中.

Everything is still being put in the AppDelegate.

//Removing the input bar above the keyboard.
@interface InputHider : NSObject @end
@implementation InputHider
-(id)inputAccessoryView{
    return nil;
}
@end

@interface UIWebBrowserView : NSObject
@end
@interface NSObject (UIWebBrowserView_Additions)
@end
@implementation NSObject (UIWebBrowserView_Additions)
- (UIKeyboardAppearance) keyboardAppearance{
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    BOOL switchOn = [userDefaults boolForKey:@"darkKeyboard"];

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

        // Locate UIWebFormView.
        for (UIView *possibleFormView in [keyboardWindow subviews]) {

            if ([possibleFormView isKindOfClass:NSClassFromString(@"UIInputSetContainerView")] ||
                [possibleFormView isKindOfClass:NSClassFromString(@"UIInputSetHostView")]) {
                for (UIView* peripheralView in possibleFormView.subviews) {
                    peripheralView.backgroundColor = [UIColor colorWithRed:0.271 green:0.271 blue:0.271 alpha:1.0];

                    //Keyboard background
                    for (UIView* peripheralView_sub in peripheralView.subviews) {
                        peripheralView_sub.backgroundColor = [UIColor colorWithRed:0.271 green:0.271 blue:0.271 alpha:1.0];

                        //Accessory bar color
                        if ([possibleFormView isKindOfClass:NSClassFromString(@"WKContentView")]) {
                            for (UIView* UIInputViewContent_sub in peripheralView_sub.subviews) {
                                [[UIInputViewContent_sub layer] setOpacity : 1.0];
                                UIInputViewContent_sub.backgroundColor = [UIColor colorWithRed:0.271 green:0.271 blue:0.271 alpha:1.0];

                            }
                        }
                    }
                }
            }
        }
        return UIKeyboardAppearanceDark;
    }
    else {
        UIWindow *keyboardWindow = nil;
        for (UIWindow *testWindow in [[UIApplication sharedApplication] windows]) {
            if (![[testWindow class] isEqual : [UIWindow class]]) {
                keyboardWindow = testWindow;
                break;
            }
        }

        // Locate UIWebFormView.
        for (UIView *possibleFormView in [keyboardWindow subviews]) {

            if ([possibleFormView isKindOfClass:NSClassFromString(@"UIInputSetContainerView")] || [possibleFormView isKindOfClass:NSClassFromString(@"UIKeyboard")]) {
                for (UIView* peripheralView in possibleFormView.subviews) {
                    peripheralView.backgroundColor = [UIColor clearColor];

                    //Keyboard background
                    for (UIView* peripheralView_sub in peripheralView.subviews) {
                        peripheralView_sub.backgroundColor = [UIColor clearColor];

                        //Accessory bar color
                        if ([possibleFormView isKindOfClass:NSClassFromString(@"UIWebFormAccessory")]) {
                            for (UIView* UIInputViewContent_sub in peripheralView_sub.subviews) {
                                [[UIInputViewContent_sub layer] setOpacity : 1.0];
                                UIInputViewContent_sub.backgroundColor = [UIColor clearColor];

                            }
                        }
                    }
                }
            }
        }
        return UIKeyboardAppearanceDefault;
    }
}
@end

@interface UITextInputTraits : UIWebBrowserView
@end
@interface UITextInputTraits (UIWebBrowserView)
@end
@implementation UITextInputTraits (UIWebBrowserView)
- (UIKeyboardAppearance) keyboardAppearance{
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    BOOL switchOn = [userDefaults boolForKey:@"darkKeyboard"];

    if (switchOn) {
        return UIKeyboardAppearanceDark;
    }
    else {
        return UIKeyboardAppearanceDefault;
    }
}
@end

//Disables endDisablingInterfaceAutorotationAnimated error for keyboard
@interface UIWindow (UIWebBrowserView)
- (void)beginDisablingInterfaceAutorotation;
- (void)endDisablingInterfaceAutorotation;
@end

@implementation UIWindow (UIWebBrowserView)
- (void)beginDisablingInterfaceAutorotation {}
- (void)endDisablingInterfaceAutorotation{}
@end

我还没有像以前那样找到隐藏 inputAccessoryBar 的方法,但是多亏了几个线程,我才能让它工作.在我的视图控制器中,我调用:

I haven't managed to find a way to hide the inputAccessoryBar like I did before, but thanks to a couple threads I got it working. In my view controllers I call:

-(void)removeInputAccessoryView {
    UIView* subview;

    for (UIView* view in webView.scrollView.subviews) {
        if([[view.class description] hasPrefix:@"WKContent"])
            subview = view;
    }

    if(subview == nil) return;

    NSString* name = [NSString stringWithFormat:@"%@SwizzleHelper", subview.class.superclass];
    Class newClass = NSClassFromString(name);

    if(newClass == nil)
    {
        newClass = objc_allocateClassPair(subview.class, [name cStringUsingEncoding:NSASCIIStringEncoding], 0);
        if(!newClass) return;

        Method method = class_getInstanceMethod([AppDelegate class], @selector(inputAccessoryView));
        class_addMethod(newClass, @selector(inputAccessoryView), method_getImplementation(method), method_getTypeEncoding(method));

        objc_registerClassPair(newClass);
    }

    object_setClass(subview, newClass);
}

在 viewDidLoad 中我调用:

And in the viewDidLoad I call:

[self removeInputAccessoryView];

我打算再修修补补,但就目前而言,这适合我需要它做的事情.

I plan on tinkering around some more, but for now, this works to what I need it to do.

这篇关于自定义 webview 键盘问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆