在 WKWebView 中隐藏键盘辅助栏 [英] Hiding Keyboard accessorybar in WKWebView

查看:19
本文介绍了在 WKWebView 中隐藏键盘辅助栏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How can I hide keyboard accessory bar in WKWebview? Although there are some resources for UIWebView, I haven't been able to find one for WkWebview on Stackoverflow.

相关:

  1. 删除 UIWebView 键盘上的配件
  2. iOS 7 UIWebView 键盘问题

推荐答案

这可以在 WKWebView 中使用 UIWebView上调出inputAccessoryView的方法.

This is possible in WKWebView with a variant of the method for swizzling out the inputAccessoryView on UIWebView.

首先,添加这个小类:

@interface _NoInputAccessoryView : NSObject

@end

@implementation _NoInputAccessoryView

- (id)inputAccessoryView {
    return nil;
}

@end

接下来,添加这个方法:

Next, add this method:

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

Then all you have to do is call that method and pass in your WKWebView:

[self removeInputAccessoryViewFromWKWebView:webView];

注意:我还不确定这是否会通过应用审核,但它非常与我用于UIWebView的相同代码,并且确实如此通过审核.

Note: I do not yet know for sure if this will pass app review, but it is extremely similar to the same code I used for UIWebView, and that did pass review.

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

Update: This code is in an app that has passed App Store review.

这篇关于在 WKWebView 中隐藏键盘辅助栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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