【问题标题】:Removing the accesory item on the UIWebView keyboard移除 WebView 键盘上的附件项
【发布时间】:2012-04-15 18:47:31
【问题描述】:

当 UIWebView 显示键盘时,顶部有一个小工具栏(附件项),上面有“完成、上一个、下一个”按钮

目前,我正在使用以下解决方法来删除此工具栏:

https://gist.github.com/2048571

但是,我担心这可能不适用于未来的 iOS 版本。有没有更好的方法来做到这一点?

【问题讨论】:

  • 您找到解决方案了吗?
  • 很遗憾,我还在用这个方法。
  • 我尝试了您在此处发布的方法,但它不适用于 ios 4.3。我找到了这个:stackoverflow.com/questions/8470984/… 它在 iOS 4.3 及更高版本中运行良好。
  • 有趣,我已经在 iOS5 中使用了这种方法。我可能已经调整了几行,如果您有兴趣,我可以尝试弄清楚我做了什么。

标签: ios


【解决方案1】:

首先,监听键盘事件:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];

当键盘显示时,移除工具栏:

- (void)keyboardWillShow:(NSNotification *)note {
[self performSelector:@selector(removeBar) withObject:nil afterDelay:0];
}

removeBar如下:

- (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 webView] scrollView];
                    } else {
                        webScroll = [[[self webView] 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];
                }
            }
        }
    }
}

参考:https://github.com/don/KeyboardToolbarRemover/blob/master/src/ios/KeyboardToolbarRemover.m

【讨论】:

  • 这是使用私有 API,因此苹果可能不会批准它,也可能会被苹果删除,因为它本身已经在问题中提及。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-06
  • 1970-01-01
  • 1970-01-01
  • 2017-03-24
  • 2021-09-04
相关资源
最近更新 更多