UIWebView 中的 UIKeyboardAppearance [英] UIKeyboardAppearance in UIWebView

查看:14
本文介绍了UIWebView 中的 UIKeyboardAppearance的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 iOS7 中,我们看到了 UIKeyboardAppearance.应用于 UITextView 时效果很好,但是,作为 Apple 声明UIWebView 不符合 UITextInputTraits 协议.

With iOS7, we saw the introduction of UIKeyboardAppearance. Works great when applied to UITextView, but, as Apple states, the UIWebView does not conform to the UITextInputTraits protocol.

虽然 UIWebView 类不直接支持 UITextInputTraits 协议,但是你可以为文本输入元素配置一些键盘属性.例如,您可以在 input 元素的定义中包含 autocorrect 和 auto-capitalization 属性来指定键盘的行为,如下例所示.

Although the UIWebView class does not support the UITextInputTraits protocol directly, you can configure some keyboard attributes for text input elements. For example, you can include autocorrect and auto-capitalization attributes in the definition of an input element to specify the keyboard’s behaviors, as shown in the following example.

有没有人想出为键盘外观设置的神奇 HTML 属性?或者没有一个?任何解决方法?(请不要使用私有 API)

Has anyone figured out the magical HTML attribute to set for keyboard appearance yet? Or is there not one? Any workaround? (No private APIs, please)

推荐答案

一个非常简单的解决方案是通过类别扩展向 UIView- (UIKeyboardAppearance) keyboardAppearance 方法>.在此方法中,您可以简单地返回 UIKeyboardAppearanceDark.这是有效的,因为该方法会神奇地添加到内部 UIWebView 视图 (UIWebBrowserView) 中,当用户点击 HTML 表单输入字段时,该视图将成为第一响应者.这种方法的主要问题是它会影响所有 UIView 派生的视图,这可能是不可取的.

A very easy solution would be to add a - (UIKeyboardAppearance) keyboardAppearance method via category extension to UIView. In this method you could simply return UIKeyboardAppearanceDark. This works because the method would magically be added to the internal UIWebView view (UIWebBrowserView) which becomes the first responder when the user taps into a HTML form input field. The main problem with this approach is that it will affect all UIView-derived views, which may not be desirable.

我们可以构建一个更有针对性的解决方案,拦截负责键盘的第一响应者,如果不存在,则为其添加一个 keyboardAppearance 方法.如果将来 UIWebBrowserView 的内部实现更改为包含 keyboardAppearance 选择器,这将优雅地降级.

We can construct a more targeted solution which intercepts the first responder that is responsible for the keyboard and adds a keyboardAppearance method to it if one doesn't exist. This will degrade gracefully if the internal implementation of UIWebBrowserView changes in the future to include a keyboardAppearance selector.

#import <objc/runtime.h>

@protocol TSPingPong <NSObject>
- (void) ts_pong: (id) sender;
@end

@interface NSObject (TSPingPong)
@end
@implementation NSObject (TSPingPong)
- (void) ts_ping: (id) sender
{
    if ( [sender respondsToSelector: @selector(ts_pong:)] )
    {
        [sender performSelector: @selector( ts_pong: ) withObject: self ];
    }
}
@end

@implementation TSViewController
{
    IBOutlet UIWebView* _webView;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    [[NSNotificationCenter defaultCenter] addObserver: self
                                             selector: @selector(keyboardWillAppear:)
                                                 name: UIKeyboardWillShowNotification
                                               object: nil];

    NSString* html = @"<br><br><br><form action=''> " 
    "First name: <input type='text'><br> " 
    "Last name: <input type='text'><br>" 
    "<input type='submit' value='Submit'> " 
    "</form>";

    [_webView loadHTMLString: html
                     baseURL: nil];
}

- (void) keyboardWillAppear: (NSNotification*) n
{
    // the keyboard is about to appear!
    // play pingpong with the first responder so we can ensure it has a keyboardAppearance method:

    [[UIApplication sharedApplication] sendAction: @selector( ts_ping:) // added via category extension
                                               to: nil                  // to: the first responder
                                             from: self                 // "sender"
                                         forEvent: nil];
}

- (void) ts_pong: (id) sender
{
    // sender is the first responder.  Happens to be undocumented "UIWebBrowserView" in this case.

    // if it doesn't have it's own keyboardAppearance method then let's add one:
    if ( ![sender respondsToSelector: @selector(keyboardAppearance)] )
    {
        Method m = class_getInstanceMethod( [self class], @selector( keyboardAppearanceTemplateMethod ) );

        IMP imp = method_getImplementation( m );

        const char* typeEncoding = method_getTypeEncoding( m );

        class_addMethod( [sender class], @selector(keyboardAppearance), imp, typeEncoding );
    }
}

- (UIKeyboardAppearance) keyboardAppearanceTemplateMethod
{
    return UIKeyboardAppearanceDark;
}

@end

这篇关于UIWebView 中的 UIKeyboardAppearance的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆