Hide shortcut keyboard bar for UIWebView in iOS 9

匿名 (未验证) 提交于 2019-12-03 02:49:01

问题:

I'm developing PhoneGap application for iOS and I need to get rid of new iOS 9 shortcut bar. Now I'm doing the following in the - (void)viewDidLoad method

if ([self.webView respondsToSelector:@selector(inputAssistantItem)]) {     UITextInputAssistantItem *inputAssistantItem = [self.webView inputAssistantItem];     inputAssistantItem.leadingBarButtonGroups = @[];     inputAssistantItem.trailingBarButtonGroups = @[]; } 

This hides undo/redo and copy/paste buttons but shortcut bar still presented on keyboard and has navigation buttons on it.

How can I get rid of shortcut bar completely.

Thanks for your help!


*** UPDATE 1 ***

My full working code is below. Hope this can help someone (thanks to @Clement reply)

#import <objc/runtime.h>  - (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);          }     } }  - (void)viewDidLoad {     [super viewDidLoad];      [self hideKeyboardShortcutBar:self.webView]; } 

This trick will hide undo/redo and navigation buttons. But auto-prediction text still will be shown on keyboard. To hide shortcut bar completely add html attributes to your input element

<input autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" /> 

P.S My app is on the review now. Still don't know if Apple will approve this.


*** UPDATE 2 ***

My app was approved by Apple

回答1:

Using method swiziling we can remove the keyboard shortcut bar (only works with ObjC).

 - (void)hideKeyboardShortcutBar     {         Class webBrowserClass = NSClassFromString(@"UIWebBrowserView");         Method method = class_getInstanceMethod(webBrowserClass, @selector(inputAccessoryView));          IMP newImp = imp_implementationWithBlock(^(id _s) {             if ([self.webView respondsToSelector:@selector(inputAssistantItem)]) {                 UITextInputAssistantItem *inputAssistantItem = [self.webView inputAssistantItem];                 inputAssistantItem.leadingBarButtonGroups = @[];                 inputAssistantItem.trailingBarButtonGroups = @[];             }             return nil;         });          method_setImplementation(method, newImp);     } 

inputAccessoryView : This property is typically used to attach an accessory view to the system-supplied keyboard that is presented for UITextField and UITextView objects.

So the new implementation block will be fired every time the keyboard pops up.

UPDATE

To remove the accessory view from WKWebView use WKContentView instead of UIWebBrowserView



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!