当点击UIWebViews时,键盘被解雇

时间:2015-07-09 09:19:20

标签: ios objective-c iphone swift uiwebview

我有一些用于富文本编辑的UIWebViews。他们有一个contentEditable的div。问题是当在字段之间切换时,如果用户点击UIWebView,键盘将在一瞬间被解雇。

从UIWebView转到本机UITextView,键盘仍然显示。

即使点击 contentEditable的UIWebView,键盘也会被解雇。在这种情况下,我可以将userInteractionEnabled转为false,但如果我希望它可滚动,那仍然无效。

这是一个证明问题的gif:

UIWebView Keyboard Dismissal

除了将以下HTML加载到UIWebViews中之外,没有任何特殊代码可以执行此操作(将第二个内容删除为contentEditable)

<!DOCTYPE html>
<html lang="ja">
    <head>
        <meta name="viewport" content="user-scalable=no">
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    </head>
    <body>
        <div id="editor" contentEditable="true"></div>
    </body>
</html>

2 个答案:

答案 0 :(得分:0)

据我所知,键盘在第一响应者停止时被解雇。您可以使用...shouldEndEditing:方法,使其返回NO以保持键盘可见。

希望有所帮助

答案 1 :(得分:0)

事实证明,在为互联网搜索了一周以及相关问题之后,我最终找到了一个重复的问题!我已this postdiegoreymendez的相关代码重新发布。

的UIWebView + GUIFixes.h

#import <UIKit/UIKit.h>

@interface UIWebView (GUIFixes)

/**
 *  @brief      Wether the UIWebView will use the fixes provided by this category or not.
 */
@property (nonatomic, assign, readwrite) BOOL usesGUIFixes;

@end

的UIWebView + GUIFixes.m

#import "UIWebView+GUIFixes.h"
#import <objc/runtime.h>

@implementation UIWebView (GUIFixes)

static const char* const fixedClassName = "UIWebBrowserViewMinusAccessoryView";
static Class fixClass = Nil;

- (UIView *)browserView
{
    UIScrollView *scrollView = self.scrollView;

    UIView *browserView = nil;
    for (UIView *subview in scrollView.subviews) {
        if ([NSStringFromClass([subview class]) hasPrefix:@"UIWebBrowserView"]) {
            browserView = subview;
            break;
        }
    }

    return browserView;
}

- (BOOL)delayedBecomeFirstResponder
{
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.01 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [super becomeFirstResponder];
    });

    return YES;
}

- (void)ensureFixedSubclassExistsOfBrowserViewClass:(Class)browserViewClass
{
    if (!fixClass) {
        Class newClass = objc_allocateClassPair(browserViewClass, fixedClassName, 0);
        objc_registerClassPair(newClass);

        IMP delayedFirstResponderImp = [self methodForSelector:@selector(delayedBecomeFirstResponder)];
        Method becomeFirstResponderMethod = class_getInstanceMethod(browserViewClass, @selector(becomeFirstResponder));
        method_setImplementation(becomeFirstResponderMethod, delayedFirstResponderImp);

        fixClass = newClass;
    }
}

- (BOOL)usesGUIFixes
{
    UIView *browserView = [self browserView];
    return [browserView class] == fixClass;
}

- (void)setUsesGUIFixes:(BOOL)value
{
    UIView *browserView = [self browserView];
    if (browserView == nil) {
        return;
    }

    [self ensureFixedSubclassExistsOfBrowserViewClass:[browserView class]];

    if (value) {
        object_setClass(browserView, fixClass);
    }
    else {
        Class normalClass = objc_getClass("UIWebBrowserView");
        object_setClass(browserView, normalClass);
    }

    [browserView reloadInputViews];
}

@end

像这样处理objc运行时感到肮脏和脆弱,但据我所知,它是实际工作的唯一方式。