【问题标题】:IQKeyboardManagerSwift toolbar not disabled in WebViewIQKeyboardManagerSwift 工具栏未在 WebView 中禁用
【发布时间】:2018-09-27 04:09:14
【问题描述】:

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

IQKeyboardManager.sharedManager().disabledToolbarClasses = []

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

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

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

【问题讨论】:

  • 它实际上不支持 WebView,因为 webview 有它们的 html textFields,既不是 UITextField 也不是 UITextView。参考这个github.com/hackiftekhar/IQKeyboardManager/issues/1207
  • 我面临的类似问题不是针对 Web 视图,而是针对从另一个库实例化的控制器。我还不想编辑库并尝试禁用它,但问题仍然存在。
  • @Vinaykrishnan 感谢您提供问题链接,是的,这是同一个问题。问题是它实际上显示了工具栏,如果错误是 t 没有显示工具栏,那么它会很容易。现在我能想到的唯一替代方案是更改库。
  • 看起来是这样,因为他们没有提供任何解决方案。

标签: ios swift webview iqkeyboardmanager


【解决方案1】:

键盘上方的工具栏是 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;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-02
    • 1970-01-01
    • 2020-08-17
    • 1970-01-01
    相关资源
    最近更新 更多