自定义 webview 键盘问题

Posted

技术标签:

【中文标题】自定义 webview 键盘问题【英文标题】:Custom webview keyboard issues 【发布时间】:2015-04-24 06:10:26 【问题描述】:

所以从这个线程 UIKeyboardAppearance in UIWebView 和 TomSwift's 调整代码真棒答案,我得到了大约 99% 的工作。

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

我的问题是,如何防止和/或改变白色部分?

其他线程中的所有代码都是相同的,除了我在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];

            
        
    

任何帮助将不胜感激。

【问题讨论】:

【参考方案1】:

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

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

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

2015 年 12 月 19 日更新 所以我决定从 UIWebView 过渡到 WKWebView,结果发现显然两者之间的事情是不同的。我设法让它再次工作。常规 UIKeyboardAppearanceDark 调用会导致键盘比我喜欢的更透明。所以我根据自己的喜好对其进行了修改。再说一次,可能不是标准的做事方式,但我不在乎,反正它不会适合苹果。

一切仍在 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 的方法,但多亏了几个线程,我让它工作了。在我的视图控制器中,我调用:

-(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 中调用:

[self removeInputAccessoryView];

我打算再做一些修改,但现在,这可以满足我的需要。

【讨论】:

不幸的是,Apple 拒绝了该应用程序,因为它们不允许从非公共类 (UIWebBrowserView) 继承。所以我们不能用这个:-( 相反,您可以使用***.com/a/23398487/2603965中描述的方法 “@interface UITextInputTraits : UIWebBrowserView”是什么意思? UITextInputTraits 已经存在并且不继承自 UIWebBrowserView,那么它有什么作用呢? 从 UITextInputTraits 继承现在被 Apple 拒绝并使用 ITMS-90338:非公共 API 使用。需要替代品。【参考方案2】:

WKWebView 键盘

这是一个使用 swizzling 的 WKWebView 的解决方案,并且相当容易合并并适用于 iOS 9、10 和 11。只需创建一个名为 WKKeyboard 的新类并添加以下代码:

WKKeyboard.h

#import <Foundation/Foundation.h>
#import <WebKit/WebKit.h>

@interface WKKeyboard : NSObject

+ (void)setStyle:(UIKeyboardAppearance)style on:(WKWebView *)webView;

@end

WKKeyboard.m

#import "WKKeyboard.h"
#import <objc/runtime.h>

@implementation WKKeyboard

// Allows the changing of keyboard styles
static UIKeyboardAppearance keyboardStyle;

// Leave this as an instance method
- (UIKeyboardAppearance)keyboardAppearance 
    return keyboardStyle;


// This can be a class method
+ (void)setStyle:(UIKeyboardAppearance)style on:(WKWebView *)webView 
    for (UIView *view in [[webView scrollView] subviews]) 
        if([[view.class description] containsString:@"WKContent"]) 
            UIView *content = view;
            NSString *className = [NSString stringWithFormat:@"%@_%@",[[content class] superclass],[self class]];
            Class newClass = NSClassFromString(className);
            if (!newClass) 
              newClass = objc_allocateClassPair([content class], [className cStringUsingEncoding:NSASCIIStringEncoding], 0);
              Method method = class_getInstanceMethod([WKKeyboard class], @selector(keyboardAppearance));
              class_addMethod(newClass, @selector(keyboardAppearance), method_getImplementation(method), method_getTypeEncoding(method));
              objc_registerClassPair(newClass);
            
            object_setClass(content, newClass);
            keyboardStyle = style;
            return;
        
    


@end

用法

// The WKWebView you want to change the keyboard on
WKWebView *webView = [WKWebView alloc] init];

// Then just call the class method with the style and webview
[WKKeyboard setStyle:UIKeyboardAppearanceDark on:webView];

希望这对某人有所帮助,这样您就可以有选择地更改单个 WKWebView 的外观,而不是全部!

【讨论】:

很高兴知道还有另一种选择。对我来说,我的回答对整个应用程序都按预期工作。很高兴为 webview 的单个实例提供一个选项。 很好的答案。尽管完成建议栏保持亮光仍然存在问题。关于如何修改它的任何建议?【参考方案3】:

@Asleepace's 答案的 Swift 版本。它对我有用。方法调配是一种方法。

class WKKeybaord: NSObject 
static var keyboardStyle: UIKeyboardAppearance = .default

@objc func keyboardAppearance() -> UIKeyboardAppearance 
    return WKKeybaord.keyboardStyle


class func setStyle(with style: UIKeyboardAppearance, on webView: WKWebView) 
    for view in webView.scrollView.subviews 
        if view.self.description.contains("WKContent") 
            let content = view
            var className: String? = nil
            if let superclass = content.self.superclass 
                className = "\(superclass)_\(type(of: self))"
            
            var newClass: AnyClass? = NSClassFromString(className ?? "")
            if newClass == nil 
                newClass = objc_allocateClassPair(object_getClass(content), className ?? "", 0)
                if let method = class_getInstanceMethod(WKKeybaord.self, #selector(self.keyboardAppearance)) 
                    class_addMethod(newClass, #selector(self.keyboardAppearance), method_getImplementation(method), method_getTypeEncoding(method))
                    objc_registerClassPair(newClass!)
                
            
            object_setClass(content, newClass!)
            keyboardStyle = style
            return
        
    


【讨论】:

【参考方案4】:

虽然让它工作起来会很好,但我决定采取完全放弃它的方法,而是使用水龙头来关闭键盘。

我的印象是隐藏它也会隐藏自动更正栏。事实并非如此。

以下是我正在使用的完整代码。

键盘的最终颜色代码,由我的应用设置中的切换开关调用。

NSUserDefaults *darkDefaults = [NSUserDefaults standardUserDefaults];
BOOL darkOn = [darkDefaults boolForKey:@"darkKeyboard"];

if (darkOn) 
    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")]) 
            for (UIView* peripheralView in possibleFormView.subviews) 

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

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")]) 
            for (UIView* peripheralView in possibleFormView.subviews) 

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

隐藏键盘。在我的视图控制器顶部调用(可能不是苹果安全的,但我不需要发布,所以它对我有用):

@interface UIWebBrowserView : UIView
@end

@implementation UIWebBrowserView (CustomToolbar)
- (id)inputAccessoryView 
    return nil;

@end

现在从我的测试来看,我可以通过在 inputAccessoryView 部分中绘制一个新视图或工具栏来为它着色,但是点击以消除它的混乱,需要进行一些调整,但这不是我的正常标准栏去。哦,好吧。

如果您想像我在表格视图中所做的那样实现切换,我就是这样做的。

- (id)init
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(reloadTableView:)
                                                 name:NSUserDefaultsDidChangeNotification object:nil];

    return [self initWithStyle:UITableViewStyleGrouped];

cellForRowAtIndexPath:

                [cell.textLabel setText:@"Dark Keyboard"];
                cell.textLabel.textAlignment = NSTextAlignmentLeft;
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
                darkKeyboard = [[UISwitch alloc] initWithFrame: CGRectMake(7, 0, 0, 0)];
                cell.accessoryView = [[UIView alloc] initWithFrame:darkKeyboard.frame];
                [cell.accessoryView addSubview:darkKeyboard];
                [self.darkKeyboard addTarget:self action:@selector(updateSwitchAtIndexPath:) forControlEvents:UIControlEventValueChanged];

                //On Color
                darkKeyboard.onTintColor = [UIColor colorWithRed:0.204 green:0.667 blue:0.863 alpha:0.85];
                //Off Color
                darkKeyboard.backgroundColor = [UIColor colorWithRed:0.678 green:0.161 blue:0.188 alpha:0.75];
                darkKeyboard.TintColor = [UIColor clearColor];
                darkKeyboard.layer.cornerRadius = 16.0;
                //Risize
                darkKeyboard.transform = CGAffineTransformMakeScale(1.1, 1.1);
                //User defaults
                darkKeyboard.on = [[NSUserDefaults standardUserDefaults] boolForKey:@"darkKeyboard"];

                UIView *keyboard = [[UIView alloc] initWithFrame:cell.frame];
                keyboard.backgroundColor = [UIColor colorWithRed:0.176 green:0.176 blue:0.176 alpha:1];
                cell.backgroundView = keyboard;

didSelectRowAtIndexPath: 刚刚加了个NSLog,这里什么都不需要。

- (void)updateSwitchAtIndexPath:(id)sender 

    if (darkKeyboard)
        NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
        [userDefaults setBool:self.darkKeyboard.on forKey:@"darkKeyboard"];
        [userDefaults synchronize];

        if ([darkKeyboard isOn]) 
            [darkKeyboard setOn:YES animated:YES];
            [self.tableView reloadData];
            [[UIApplication sharedApplication] reloadInputViews];

         else 
            [darkKeyboard setOn:NO animated:YES];
            [self.tableView reloadData];
            [[UIApplication sharedApplication] reloadInputViews];
        
    


希望这对某人有所帮助。

【讨论】:

【参考方案5】:

像下面这样扩展 UIWebBrowserView,会让你的应用被应用商店禁止。

@interface UIWebBrowserView : UIView
@end

@implementation UIWebBrowserView (KeyboardSwitch)
- (UIKeyboardAppearance) keyboardAppearance
    return UIKeyboardAppearanceDark;

@end

因此,您需要在运行时扩展 Web 视图。

以下代码以与 iOS12 兼容且不会被 Apple 拒绝的方式实现这一点。 对于此示例,我使用全局 _s_isDark 来确定所需的键盘样式。

@implementation UIWebView (KeyboardAppearanceAndAccessoryHiding)

- (void) setModifiedWebviewView 
    // find the UIWebBrowserView
    for (UIView *browserView in self.scrollView.subviews) 
        if ([NSStringFromClass([browserView class]) hasPrefix:@"UIWebBrowserView"]) 
            // Allocate a UIWebBrowserView subclass
            Class newClass = objc_allocateClassPair([browserView class], "UIWebBrowserModified", 0);

            // Add a nil method to hide the accessory view
            IMP nilImp = [self methodForSelector:@selector(methodReturningNil)];
            class_addMethod(newClass, @selector(inputAccessoryView), nilImp, "@@:");

            // Add a method to set dark or light keyboard
            Method m = class_getInstanceMethod([self class], @selector(keyboardAppearance));
            IMP keyboardAppearanceImp = method_getImplementation(m);
            const char* typeEncoding = method_getTypeEncoding(m);
            class_addMethod(newClass, @selector(keyboardAppearance), keyboardAppearanceImp, typeEncoding);

            // Replace the class of the UIWebBrowserView with the new subclass
            objc_registerClassPair(newClass);
            object_setClass(browserView, newClass);
            break;
        
    


- (id)methodReturningNil 
    return nil;


- (UIKeyboardAppearance)keyboardAppearance 
    return _s_isDark ? UIKeyboardAppearanceDark : UIKeyboardAppearanceLight;


@end

// We also need to extend the text input traits
@interface UITextInputTraits
@end
@interface UITextInputTraits (ForWebViewFields)
@end
@implementation UITextInputTraits (ForWebViewFields)
- (UIKeyboardAppearance)keyboardAppearance 
    return _s_isDark ? UIKeyboardAppearanceDark : UIKeyboardAppearanceLight;

@end

更新:从 2020 年 2 月起,扩展 UITextInputTraits 也会让你被 App Store 禁止(使用 ITMS-90338:非公共 API 使用)。我不确定是否需要继承 UITextInputTraits。

【讨论】:

【参考方案6】:

另一种答案。

因此,无需尝试定义另一种颜色,只需使用上述开关获得浅色或深色外观即可。我将此添加到我的 AppDelegate,(有些人不喜欢在这里添加东西,但我的应用程序中有这么多视图,这是必须的)。

@interface AppDelegate ()上方添加这个

@interface UIWebBrowserView : UIView
@end

@implementation UIWebBrowserView (KeyboardSwitch)

- (UIKeyboardAppearance) keyboardAppearance

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

    if (switchOn) 
        return UIKeyboardAppearanceDark;
    
    else 
        return UIKeyboardAppearanceDefault;
    

@end

完美运行。可能不会被 Apple 接受,但我对此没有兴趣。

现在适合那些不想创建切换而只想要深色键盘的人。

@interface UIWebBrowserView : UIView
@end

@implementation UIWebBrowserView (KeyboardSwitch)

- (UIKeyboardAppearance) keyboardAppearance

    return UIKeyboardAppearanceDark;

@end

更新: 使用自定义颜色:在模拟器中从 iOS 7-8.2 测试。

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

        for (UIView *possibleFormView in [keyboardWindow subviews]) 
            if ([possibleFormView isKindOfClass:NSClassFromString(@"UIInputSetContainerView")] || [possibleFormView isKindOfClass:NSClassFromString(@"UIPeripheralHostView")]) 
                for (UIView* peripheralView in possibleFormView.subviews) 

                    //Keyboard background
                    for (UIView* peripheralView_sub in peripheralView.subviews) 
                        //Setting custom color
                        peripheralView_sub.backgroundColor = [UIColor colorWithRed:0.271 green:0.271 blue:0.271 alpha:0.50];
                    
                
            
        
        return UIKeyboardAppearanceDark;
    
    else 
        UIWindow *keyboardWindow = nil;
        for (UIWindow *testWindow in [[UIApplication sharedApplication] windows]) 
            if (![[testWindow class] isEqual : [UIWindow class]]) 
                keyboardWindow = testWindow;
                break;
            
        

        for (UIView *possibleFormView in [keyboardWindow subviews]) 
            if ([possibleFormView isKindOfClass:NSClassFromString(@"UIInputSetContainerView")] || [possibleFormView isKindOfClass:NSClassFromString(@"UIPeripheralHostView")]) 
                for (UIView* peripheralView in possibleFormView.subviews) 

                    //Keyboard background
                    for (UIView* peripheralView_sub in peripheralView.subviews) 
                        //Clear color so it doesn't show when switching with toggle
                        peripheralView_sub.backgroundColor = [UIColor clearColor];
                    
                
            
        
        return UIKeyboardAppearanceDefault;
    

@end

希望这对未来的 webview 开发人员有所帮助。

【讨论】:

这将被 Apple 拒绝。他们不允许以这种方式使用私有 UIWebBrowserView。您必须使用 Objective-C 运行时函数。

以上是关于自定义 webview 键盘问题的主要内容,如果未能解决你的问题,请参考以下文章

如何从自定义键盘扩展启动包含应用程序?

自定义 Webview 上的 ClassCastException

Flutter中 webview的键盘问题解决

Flutter,WebView - 使用自定义 HTML 重建

Webview 的自定义“撤消”

如何在现有的 webview android 中添加自定义按钮?