WebView

时间:2018-04-17 09:01:04

标签: ios swift webview iqkeyboardmanager

我在我的应用中使用IQKeyboardManagerSwift。它在一些ViewControllers中工作得很好我必须禁用它的工具栏。根据文档,我应该添加viewcontrollers,我想在下面的数组中禁用工具栏。

IQKeyboardManager.sharedManager().disabledToolbarClasses = []

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

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

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

enter image description here

1 个答案:

答案 0 :(得分:4)

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

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

ViewController.h

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

ViewController.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;
}