iOS 去除 Webview 键盘顶部工具栏

栏目: IOS · 发布时间: 5年前

内容简介:在默认情况下,iOS 使用 Webview 打开的网页,在进行表单输入时,弹出的键盘顶部会多出一个工具栏。左边有两个上下按钮,右边有一个为了让 App 中嵌入的 H5 更接近 Native,咱们可以去掉它。

在默认情况下,iOS 使用 Webview 打开的网页,在进行表单输入时,弹出的键盘顶部会多出一个 工具 栏。

左边有两个上下按钮,右边有一个 Done/完成 按钮。这是用来切换输入框的,就像 PC 上按 Tab 键可以切换输入框一样。

为了让 App 中嵌入的 H5 更接近 Native,咱们可以去掉它。

UIWebView

UIWebView,可以使用 [self hideKeyboardShortcutBar:self.webView] 去掉工具栏。

- (void) hideKeyboardShortcutBar: (UIView *)view
{
    for (UIView *sub in view.subviews) {
        [self hideKeyboardShortcutBar:sub];
        if ([NSStringFromClass([sub class]) isEqualToString:@"UIWebBrowserView"]) {
            
            Method method = class_getInstanceMethod(sub.class, @selector(inputAccessoryView));
            IMP newImp = imp_implementationWithBlock(^(id _s) {
                if ([sub respondsToSelector:@selector(inputAssistantItem)]) {
                    UITextInputAssistantItem *inputAssistantItem = [sub inputAssistantItem];
                    inputAssistantItem.leadingBarButtonGroups = @[];
                    inputAssistantItem.trailingBarButtonGroups = @[];
                }
                return nil;
            });
            method_setImplementation(method, newImp);
            
        }
    }
}
复制代码

WkWebView

WkWebView,可以使用 [self hideWKWebviewKeyboardShortcutBar:self.webView] 去掉工具栏。

// 步骤一:创建一个 _NoInputAccessoryView
@interface _NoInputAccessoryView : NSObject
@end
@implementation _NoInputAccessoryView
- (id)inputAccessoryView {
    return nil;
}
@end

// 步骤二:去掉 WkWebviewe Done 工具栏
- (void) hideWKWebviewKeyboardShortcutBar:(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);
}
复制代码

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

链接

链接

[美] 巴拉巴西 / 徐彬 / 湖南科技出版社 / 2007-04-01 / 28.00

从鸡尾酒会到恐怖分子的巢穴,从远古的细菌到国际组织——所有这一切各自都是一种网络,都是一个令人惊讶的科学革新的一部分。21世纪初,有科学家发现,网络具有深层的秩序,依据简单而强有力的规则运行。这一领域的知识帮助我们了解时尚、病毒等的传播机制,了解生态系统的稳健性,以及经济体系的脆弱性——甚至是民主的未来。 一位致力于研究“链接和节点”的科学家将首次带领我们领略网络革新的内幕。在本书中,作者生......一起来看看 《链接》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

html转js在线工具
html转js在线工具

html转js在线工具

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具