UIWebView键盘 – 摆脱“上一页/下一页/完成”栏

我想摆脱当你在web视图中的文本字段焦点时出现的键盘上的酒吧。 我们还有其他方法来处理这个问题,这是多余的,不必要的。

webview键盘栏http://beautifulpixel.com/assets/iPhone_Simulator-20100120-152330.png

如果您遇到此问题 ,请务必前往https://bugreport.apple.com并复制rdar:// 9844216

- (void)viewDidLoad { [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil]; } - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil]; } - (void)keyboardWillShow:(NSNotification *)notification { [self performSelector:@selector(removeBar) withObject:nil afterDelay:0]; } - (void)removeBar { UIWindow *keyboardWindow = nil; for (UIWindow *testWindow in [[UIApplication sharedApplication] windows]) { if (![[testWindow class] isEqual:[UIWindow class]]) { keyboardWindow = testWindow; break; } } for (UIView *possibleFormView in [keyboardWindow subviews]) { // iOS 5 sticks the UIWebFormView inside a UIPeripheralHostView. if ([[possibleFormView description] rangeOfString:@"UIPeripheralHostView"].location != NSNotFound) { for (UIView *subviewWhichIsPossibleFormView in [possibleFormView subviews]) { if ([[subviewWhichIsPossibleFormView description] rangeOfString:@"UIWebFormAccessory"].location != NSNotFound) { [subviewWhichIsPossibleFormView removeFromSuperview]; } } } } } 

这很好。

url: http : //ios-blog.co.uk/iphone-development-tutorials/rich-text-editor-inserting-images-part-6/

这是云的回答的补充。 在iOS6(6.0.1)上,可能会在附件(上一个/下一个/完成)曾被删除之前的行顶部出现水平灰色边框或阴影线。 此修补程序适用于我,我想分享。 好奇听到它是否也适用于你。

要删除边框,我将这段代码添加到removeBar()的内部循环中:

 if ([[subviewWhichIsPossibleFormView description] rangeOfString:@"UIImageView"].location != NSNotFound) { [[subviewWhichIsPossibleFormView layer] setOpacity: 0.0]; } 

我们需要将QuartzCore框架添加到.m文件的头部,所以我们可以设置所涉及的图层的不透明度。

所以,我们得到:

 ... #import <QuartzCore/QuartzCore.h> ... - (void)removeBar { UIWindow *keyboardWindow = nil; for (UIWindow *testWindow in [[UIApplication sharedApplication] windows]) { if (![[testWindow class] isEqual:[UIWindow class]]) { keyboardWindow = testWindow; break; } } for (UIView *possibleFormView in [keyboardWindow subviews]) { // iOS 5 sticks the UIWebFormView inside a UIPeripheralHostView. if ([[possibleFormView description] rangeOfString:@"UIPeripheralHostView"].location != NSNotFound) { for (UIView *subviewWhichIsPossibleFormView in [possibleFormView subviews]) { if ([[subviewWhichIsPossibleFormView description] rangeOfString:@"UIWebFormAccessory"].location != NSNotFound) { [subviewWhichIsPossibleFormView removeFromSuperview]; } // iOS 6 leaves a grey border / shadow above the hidden accessory row if ([[subviewWhichIsPossibleFormView description] rangeOfString:@"UIImageView"].location != NSNotFound) { // we need to add the QuartzCore framework for the next line [[subviewWhichIsPossibleFormView layer] setOpacity: 0.0]; } } } } } 

看起来有一个非常简单的方法,但我很确定它不会通过App Store审查。 也许有人有一个聪明的想法? ;)

 @interface UIWebBrowserView : UIView @end @interface UIWebBrowserView (UIWebBrowserView_Additions) @end @implementation UIWebBrowserView (UIWebBrowserView_Additions) - (id)inputAccessoryView { return nil; } @end 

这样做没有公共的API。 您可以通过检查视图层次结构并按照某些人的build议删除视图来删除它,但是这样做会非常危险。

这是为什么这是一个坏主意:

如果苹果公司没有正式的API来删除它们,他们可能有很好的理由,而且他们自己的代码可能依赖于它。 您可能没有遇到任何问题,因为您在英文键盘上执行所有testing(例如)。 但是,如果您要删除的视图需要以其他语言input,或者为了便于访问,该怎么办? 或者,如果在iOS的未来版本中他们自己的实现发生了变化,那么它假设视图总是在那里? 你的代码会崩溃,你会被困在争取更新,而挫折用户等待几个星期。

有趣的是,Remco附加的答案certificate了这一点。 在iOS 6.0.1上,进行了修改,要求对黑客进行修复。 任何对ios 5进行破解的人都将被迫做出更新。 幸运的是,这只是一个美学上的改变,但情况可能会更糟糕。

我想拦截UIKeyboardWillAppear通知,并把它给一个隐藏的文本字段,并通过JavaScript转发到webview中的真实的事件。 但似乎有毛。 光标移动和select的东西就会吮吸。

看看这一个。 https://gist.github.com/2048571 。 它适用于iOS 5及更高版本,不适用于早期版本。

这个代码definetly为我工作…希望这也适用于你。

 - (void)viewDidLoad{ [super viewDidLoad]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil]; } -(void)viewWillAppear:(BOOL)animated{ [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil]; } - (void)keyboardWillShow:(NSNotification *)notification { [self performSelector:@selector(removeBar) withObject:nil afterDelay:0]; } - (void)removeBar { // Locate non-UIWindow. UIWindow *keyboardWindow = nil; for (UIWindow *testWindow in [[UIApplication sharedApplication] windows]) { if (![[testWindow class] isEqual:[UIWindow class]]) { keyboardWindow = testWindow; break; } } // Locate UIWebFormView for (UIView *possibleFormView in [keyboardWindow subviews]) { if ([[possibleFormView description] hasPrefix:@"<UIPeripheralHostView"]) { for (UIView* peripheralView in [possibleFormView subviews]) { // hides the backdrop (iOS 7) if ([[peripheralView description] hasPrefix:@"<UIKBInputBackdropView"]) { //skip the keyboard background....hide only the toolbar background if ([peripheralView frame].origin.y == 0){ [[peripheralView layer] setOpacity:0.0]; } } // hides the accessory bar if ([[peripheralView description] hasPrefix:@"<UIWebFormAccessory"]) { // remove the extra scroll space for the form accessory bar UIScrollView *webScroll; if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 5.0) { webScroll = [[self webviewpot] scrollView]; } else { webScroll = [[[self webviewpot] subviews] lastObject]; } CGRect newFrame = webScroll.frame; newFrame.size.height += peripheralView.frame.size.height; webScroll.frame = newFrame; // remove the form accessory bar [peripheralView removeFromSuperview]; } // hides the thin grey line used to adorn the bar (iOS 6) if ([[peripheralView description] hasPrefix:@"<UIImageView"]) { [[peripheralView layer] setOpacity:0.0]; } } } } } 

不容易。 你可以尝试去浏览网页视图中的子视图,但这会对苹果禁忌。

如何不把网页上的文本字段放在网页上,并显式添加textfield / textview到webview,所以它根本不显示导航栏,你可以从头开始添加自己的?

 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasShown:) name:UIKeyboardDidShowNotification object:nil]; -(void)keyboardWasShown:(NSNotification*)aNotification { UIWindow* tempWindow; //Because we cant get access to the UIKeyboard throught the SDK we will just use UIView. //UIKeyboard is a subclass of UIView anyways UIView* keyboard; //Check each window in our application for(int c = 0; c < [[[UIApplication sharedApplication] windows] count]; c ++) { //Get a reference of the current window tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:c]; //Get a reference of the current view for(int i = 0; i < [tempWindow.subviews count]; i++) { keyboard = [tempWindow.subviews objectAtIndex:i]; if([[keyboard description] hasPrefix:@"<UIPeripheralHostView"] == YES) { keyboard.hidden = YES; UIView* keyboardLayer; for(int n = 0; n < [keyboard.subviews count]; n++) { keyboardLayer = [keyboard.subviews objectAtIndex:n]; NSLog(@" keyboardLayer ::: %@ " ,keyboardLayer); if([[keyboardLayer description] hasPrefix:@"<UIWebFormAccessory"] == YES) { [keyboardLayer removeFromSuperview ]; } } keyboard.hidden = NO; } } } NSLog(@"keyboardWasShown" ); } 

检查这个以及: http : //pastebin.com/s3Fkxvsk