在iOS7中,我们看到了UIKeyboardAppearance的介绍。应用于UITextView
时效果很好,但是,UIWebView
UITextInputTraits
不符合{{1}}协议。
虽然UIWebView类不直接支持UITextInputTraits协议,但您可以为文本输入元素配置一些键盘属性。例如,您可以在input元素的定义中包含自动更正和自动大写属性,以指定键盘的行为,如以下示例所示。
有没有人想出为键盘外观设置的神奇HTML属性呢?或者没有一个?任何解决方法? (请不要使用私人API)
答案 0 :(得分:11)
一个非常简单的解决方案是通过类别扩展名向- (UIKeyboardAppearance) keyboardAppearance
添加UIView
方法。在这种方法中,您只需返回UIKeyboardAppearanceDark
即可。这是有效的,因为该方法会神奇地添加到内部UIWebView
视图(UIWebBrowserView
)中,当用户点击HTML表单输入字段时,该视图成为第一个响应者。这种方法的主要问题是它会影响所有UIView派生的视图,这可能是不可取的。
我们可以构建一个更有针对性的解决方案,拦截负责键盘的第一个响应者,并在不存在的情况下为其添加keyboardAppearance
方法。如果UIWebBrowserView
的内部实施在将来发生变化以包含keyboardAppearance
选择器,则会优雅地降级。
#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