是否可以在独立的 Web 应用程序中从 iPhone 弹出键盘中删除表单助手?我知道普遍的共识是,这在 Mobile Safari 中是不可能的,但独立应用程序在
UIWebView
中运行,并且在多种方面有不同的功能(example),所以我希望这可能是可能的。
您可以在键盘上方看到它:
上一个和下一个按钮在
<form>
输入之间循环。但我有一个 <input>
元素,所以它们被禁用。 “完成”按钮隐藏了键盘,但由于我有一个高度灵活的 <ul>
(占据了键盘和 <input>
之间的空间),而且我在该页面上没有其他内容,所以它没有任何用处。
在一个很小的屏幕上,键盘几乎占据了一半的屏幕,组成这个工具栏的 44 个像素是对空间的巨大浪费(整个
<li>
的价值)。
原生 iOS 应用程序可以删除它,所以我知道这至少可以在手机上实现,我只是还没有找到在网络应用程序中执行此操作的方法。这是来自 Facebook 应用程序,该页面与我的非常相似:
我尝试过使用未包裹在
<input>
中的 <form>
以及使用 contenteditable
<div>
,但结果是相同的。有几种自定义 -webkit-
样式可以控制 Web 应用程序界面的各个方面,但它们的文档记录很少,并且搜索没有发现任何相关内容。
有什么方法可以删除网络应用程序中的表单助手吗?
如果您的应用程序是包装在本机 Objetive-C 应用程序中的 Web 应用程序,则可以通过操作键盘视图来实现。
首先,注册接收keyboardDidShow通知:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:) name:UIKeyboardDidShowNotification object:nil];
当键盘出现时,这将调用以下方法:
-(void)keyboardDidShow:(NSNotification*)notif
{
NSArray *array = [[UIApplication sharedApplication] windows];
for (UIWindow* wind in array) {
for (UIView* currView in wind.subviews) {
if ([[currView description] hasPrefix:@"<UIPeripheralHostView"]) {
for (UIView* perView in currView.subviews) {
if ([[perView description] hasPrefix:@"<UIWebFormAccessory"]) {
[perView setHidden:YES];
}
}
}
}
}
}
此方法会遍历屏幕上的视图,查找表单助手并将其隐藏。
注意:苹果可能不会拒绝这一点,因为我已经看到 Facebook 等正在使用它,但这种技术可能会在即将发布的 iOS 版本中被打破。
您可以创建 UIView 的类别并“覆盖”addSubview 的行为:如下例所示。从 AppDelegate 的 applicationDidFinishLaunching 中调用方法“exachangeMethods”。
#import "UIView+util.h"
#import <objc/runtime.h>
@implementation UIView (util)
// Swaps our custom implementation with the default one
// +load is called when a class is loaded into the system
+ (void) exchangeMethods
{
SEL origSel = @selector(addSubview:);
SEL newSel = @selector(customAddSubview:);
Class viewClass = [UIView class];
Method origMethod = class_getInstanceMethod(viewClass, origSel);
Method newMethod = class_getInstanceMethod(viewClass, newSel);
method_exchangeImplementations(origMethod, newMethod);
}
- (void) customAddSubview:(UIView *)view{
if( [[view description]rangeOfString:@"<UIWebFormAccessory"].location!=NSNotFound) {
return;
}
// This line at runtime does not go into an infinite loop
// because it will call the real method instead of ours.
return [self customAddSubview:view];
}
@end