在学习 WKWebView
中键盘的弹出、隐藏时,碰巧看到了网友的一个需求是 隐藏 inputAccessoryView
,虽然我并不需要这么高端的操作,也不懂前端的问题,但思路碰巧帮我解决了另一个问题。🤓
1 2 3 4 5 6 7 8 9 10 11
| @interface _NoInputAccessoryView : NSObject
@end
@implementation _NoInputAccessoryView
- (id)inputAccessoryView { return nil; }
@end
|
2.实现如下方法,例如在 WKWebView 的控制器中
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| - (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); }
|
3.调用上面的方法,传入对应的 WebView
1
| [self removeInputAccessoryViewFromWKWebView:webView];
|
PS:这样的确可以隐藏 inputAccessoryView
,但是键盘的关闭就需要再单独处理一下了。😂🤓🤗
PPS: 原文链接