UIWebView中的UIKeyboardAppearance [英] UIKeyboardAppearance in UIWebView
问题描述
在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元素的定义中包含自动更正和自动大写属性,以指定键盘的行为,如以下示例所示。
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)
推荐答案
一个非常简单的解决方案是添加 - (UIKeyboardAppearance )keyboardAppearance
通过类别扩展到 UIView
的方法。在这种方法中,您只需返回 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屋!