【问题标题】:Hiding Keyboard accessorybar in WKWebView在 WKWebView 中隐藏键盘辅助栏
【发布时间】:2015-09-13 04:45:45
【问题描述】:

如何在 WKWebview 中隐藏键盘附件栏?虽然有一些 UIWebView 的资源,但我在 Stackoverflow 上找不到 WkWebview 的资源。

相关:

  1. Removing the accesory item on the UIWebView keyboard
  2. iOS 7 UIWebView keyboard issue

【问题讨论】:

    标签: ios xcode uiwebview wkwebview


    【解决方案1】:

    这可以在WKWebView 中使用the method for swizzling out the inputAccessoryView on UIWebView 的变体实现。

    首先,添加这个小类:

    @interface _NoInputAccessoryView : NSObject
    
    @end
    
    @implementation _NoInputAccessoryView
    
    - (id)inputAccessoryView {
        return nil;
    }
    
    @end
    

    接下来,添加这个方法:

    - (void)removeInputAccessoryViewFromWKWebView:(WKWebView *)webView {
        UIView *targetView;
    
        for (UIView *view in webView.scrollView.subviews) {
            if([[view.class description] hasPrefix:@"WKContent"]) {
                targetView = view;
            }
        }
    
        if (!targetView) {
            return;
        }
    
        NSString *noInputAccessoryViewClassName = [NSString stringWithFormat:@"%@_NoInputAccessoryView", targetView.class.superclass];
        Class newClass = NSClassFromString(noInputAccessoryViewClassName);
    
        if(newClass == nil) {
            newClass = objc_allocateClassPair(targetView.class, [noInputAccessoryViewClassName cStringUsingEncoding:NSASCIIStringEncoding], 0);
            if(!newClass) {
                return;
            }
    
            Method method = class_getInstanceMethod([_NoInputAccessoryView class], @selector(inputAccessoryView));
    
            class_addMethod(newClass, @selector(inputAccessoryView), method_getImplementation(method), method_getTypeEncoding(method));
    
            objc_registerClassPair(newClass);
        }
    
        object_setClass(targetView, newClass);
    }
    

    那么你所要做的就是调用那个方法并传入你的WKWebView:

    [self removeInputAccessoryViewFromWKWebView:webView];
    

    注意:我还不确定这是否会通过应用审核,但它非常与我用于UIWebView 的代码相似,并且确实通过了审核。 p>

    更新:此代码位于已通过 App Store 审核的应用中。

    【讨论】:

    • 不,这个解决方案是专门针对 WKWebView 的,虽然它是从 UIWebView 的技术改编而来的。
    • 这个答案的 Swift 版本:stackoverflow.com/questions/33853924/…
    • @JustinMichael 我正在尝试使用 11.4,我发现仅在 iPad 上,如果我执行 webView.inputAssistantItem.trailingBarButtonGroups/leadingBarButtonGroups = [NSMutableArray new];,AccessoryView 就会消失
    • 这是基于我在这里的回答:stackoverflow.com/a/19042279/983912 将不胜感激参考。
    • @LeoNatan 抱歉。当我最初发布时,我以为我链接到了你的答案,但我一定忘记了。更新了答案以链接到您的答案。感谢您大声疾呼!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-10-12
    • 2016-02-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-28
    • 2013-05-10
    • 1970-01-01
    相关资源
    最近更新 更多