UIWebView键盘 - 摆脱“上一个/下一个/完成”栏

时间:2010-01-20 23:29:10

标签: iphone ios uiwebview keyboard

我想摆脱在webview中聚焦文本字段时出现的键盘顶部的栏。我们还有其他一些方法可以解决这个问题,这是多余的,也是不必要的。

webview keyboard bar http://beautifulpixel.com/assets/iPhone_Simulator-20100120-152330.png

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

9 个答案:

答案 0 :(得分:10)

- (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/

答案 1 :(得分:5)

这是Yun回答的补充。在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];
                }
            }
        }
    }
}

答案 2 :(得分:4)

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

@interface UIWebBrowserView : UIView
@end

@interface UIWebBrowserView (UIWebBrowserView_Additions)
@end

@implementation UIWebBrowserView (UIWebBrowserView_Additions)

- (id)inputAccessoryView {
    return nil;
}

@end

答案 3 :(得分:2)

这样做没有公共API。您可以通过检查视图层次结构并删除视图,如某些人所建议的那样删除它,但这将是非常危险的。

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

如果Apple没有用于删除栏的官方API,他们可能有充分的理由这样做,并且他们自己的代码可能依赖于它在那里。您可能不会遇到问题,因为您在英语键盘上进行了所有测试(例如)。但是,如果要删除的视图是以其他语言输入或出于辅助目的而需要的?或者,如果在iOS的未来版本中,他们自己的实现会发生变化,以至于假设视图始终存在?您的代码将崩溃,并且您将陷入困境以获取更新,而受挫的用户会等待数周。

有趣的是,Remco的附加答案证明了这一点。在iOS 6.0.1上,进行了一项需要修复hack的更改。任何为ios 5实施了hack的人都会被迫进行更新。幸运的是,这只是一种审美变化,但可能会更糟糕。

答案 4 :(得分:1)

我正在考虑拦截UIKeyboardWillAppear通知,并将其提供给隐藏文本字段,并通过javascript将事件转发到webview中的真实事件。但它看起来很毛茸茸。事物光标的移动和选择会很糟糕。

答案 5 :(得分:1)

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

答案 6 :(得分:1)

这段代码绝对适用于我...希望这对你也有用。

- (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];
                }
            }
        }
    }
}

答案 7 :(得分:0)

不容易。您可以尝试在Web视图中查看子视图,但这对Apple来说是禁忌。

如何不将文本字段放在网页的网页上,并明确地将文本字段/文本视图添加到webview中,以便它根本不显示导航栏,您可以从头开始添加自己的文本字段?

答案 8 :(得分:0)

[[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