IOS - 删除WebView键盘上的工具条
2017-12-26 本文已影响50人
囧rg
UIWebView代码:
.h文件
#import "ZSSWebView.h"
@interface ZSSWebView (AccessoryHiding)
@property (nonatomic, assign) BOOL hackishlyHidesInputAccessoryView;
@end
.m文件
#import "ZSSWebView+AccessoryHiding.h"
#import <objc/runtime.h>
@implementation ZSSWebView (AccessoryHiding)
static const char * const hackishFixClassName = "UIWebBrowserViewMinusAccessoryView";
static Class hackishFixClass = Nil;
- (UIView *)hackishlyFoundBrowserView {
UIScrollView *scrollView = self.scrollView;
UIView *browserView = nil;
for (UIView *subview in scrollView.subviews) {
NSLog(@"%@",NSStringFromClass([subview class]));
if ([NSStringFromClass([subview class]) hasPrefix:@"UIWebBrowserView"]) {
browserView = subview;
break;
}
}
return browserView;
}
- (id)methodReturningNil {
return nil;
}
- (void)ensureHackishSubclassExistsOfBrowserViewClass:(Class)browserViewClass {
if (!hackishFixClass) {
Class newClass = objc_allocateClassPair(browserViewClass, hackishFixClassName, 0);
IMP nilImp = [self methodForSelector:@selector(methodReturningNil)];
class_addMethod(newClass, @selector(inputAccessoryView), nilImp, "@@:");
objc_registerClassPair(newClass);
hackishFixClass = newClass;
}
}
- (BOOL) hackishlyHidesInputAccessoryView {
UIView *browserView = [self hackishlyFoundBrowserView];
return [browserView class] == hackishFixClass;
}
- (void) setHackishlyHidesInputAccessoryView:(BOOL)value {
UIView *browserView = [self hackishlyFoundBrowserView];
if (browserView == nil) {
return;
}
[self ensureHackishSubclassExistsOfBrowserViewClass:[browserView class]];
if (value) {
object_setClass(browserView, hackishFixClass);
} else {
Class normalClass = objc_getClass("UIWebBrowserView");
object_setClass(browserView, normalClass);
}
[browserView reloadInputViews];
}
@end
WKWebView代码:
.h文件
#import "ZSSWebView.h"
@interface ZSSWebView (AccessoryHiding)
- (void)removeInputAccessoryViewFromWKWebView:(WKWebView *)webView;
@end
.m文件
#import "ZSSWebView+AccessoryHiding.h"
#import <objc/runtime.h>
@implementation ZSSWebView (AccessoryHiding)
- (id)inputAccessoryView {
return nil;
}
- (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([ZSSWebView class], @selector(inputAccessoryView));
class_addMethod(newClass, @selector(inputAccessoryView), method_getImplementation(method), method_getTypeEncoding(method));
objc_registerClassPair(newClass);
}
object_setClass(targetView, newClass);
}
@end