1

我在我的应用程序中使用 IQKeyboardManagerSwift。它在某些 ViewController 中运行良好,我必须禁用它的工具栏。根据文档,我应该添加要在以下数组中禁用工具栏的视图控制器。

IQKeyboardManager.sharedManager().disabledToolbarClasses = []

现在它可以完美地用于视图控制器中的 TextFields。但问题在于 webview 中的 TextFields。我有一个视图控制器,我必须在 WebView 内的字段中输入数据,因为 webview 工具栏内的字段没有被禁用,我尝试了很多方法。即使为整个应用禁用 IQKeyBaord (IQKeyboardManager.sharedManager().enable = false) 也不会影响 WebView

我不明白问题是什么,WebView 是否有一些特定的处理或可能的替代方法。

为了澄清,带有完成按钮的顶视图是我指的工具栏。

在此处输入图像描述

4

1 回答 1

4

键盘上的工具栏是 WebView/WKWebView 的默认属性/行为。

但是如果你想删除它,这里是代码,你可以根据你的需要修改它:

视图控制器.h

 #import <UIKit/UIKit.h>
 @interface ViewController : UIViewController
 @property (weak, nonatomic) IBOutlet UIWebView *webView;
 @end

视图控制器.m

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [[NSNotificationCenter defaultCenter]  addObserver:self selector:@selector(removeKeyboardTopBar:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter]  addObserver:self selector:@selector(removeKeyboardTopBar:) name:UIKeyboardDidShowNotification object:nil];
}

-(void)viewDidAppear:(BOOL)animated{
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"https://html5doctor.com/demos/forms/forms-example.html"]];
    [_webView loadRequest:request];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void) removeKeyboardTopBar:(NSNotification*)notify{
    // Locate non-UIWindow.
    UIWindow *keyboardWindow = nil;
    __block UIView* toolBarContainer = nil;
    NSArray* windows = [[UIApplication sharedApplication] windows];
    for (UIWindow *possibleWindow in windows) {
        if (![[possibleWindow class] isEqual:[UIWindow class]]) {
            keyboardWindow = possibleWindow;
            break;
        }
    }
        for (UIView *possibleFormView in [keyboardWindow subviews])
        {
            if([[[UIDevice currentDevice] systemVersion] floatValue]>8){
                if([[possibleFormView description] hasPrefix:@"<UIInputSetContainerView"])
                {
                    for(int i = 0 ; i < [possibleFormView.subviews count] ; i++)
                    {
                        UIView* hostkeyboard = [possibleFormView.subviews objectAtIndex:i];
                        if([[hostkeyboard description] hasPrefix:@"<UIInputSetHostView"])
                        {
                            for (id temp in hostkeyboard.subviews)
                            {
                                if ([[temp description] hasPrefix:@"<UIWebFormAccessory"])
                                {
                                    UIView* currentToolBar = (UIView*)temp;
                                    currentToolBar.hidden = true;
                                    toolBarContainer = hostkeyboard;
                                }
                            }
                        }
                    }
                }
            }else{
                if ([[possibleFormView description] rangeOfString:@"UIPeripheralHostView"].location != NSNotFound) {
                    for (UIView *subviewWhichIsPossibleFormView in [possibleFormView subviews]) {
                        if ([[subviewWhichIsPossibleFormView description] rangeOfString:@"UIWebFormAccessory"].location != NSNotFound) {
                            [subviewWhichIsPossibleFormView removeFromSuperview];
                        }
                    }
                }
            }

        }
    if(toolBarContainer){
        if([notify.name isEqualToString:@"UIKeyboardWillShowNotification"]){
            [toolBarContainer setHidden:YES];
        }else if([notify.name isEqualToString:@"UIKeyboardDidShowNotification"]){
            [toolBarContainer setHidden:NO];
        }
        dispatch_async(dispatch_get_main_queue(), ^(){                
            toolBarContainer.frame = CGRectMake(toolBarContainer.frame.origin.x,toolBarContainer.frame.origin.y+44,toolBarContainer.frame.size.width,toolBarContainer.frame.size.height);
        });
    }

    keyboardWindow = nil;
}
于 2018-04-17T10:50:36.950 回答