【问题标题】:How to hide the keyboard assistant bar如何隐藏键盘助手栏
【发布时间】:2016-06-13 23:04:08
【问题描述】:

我正在开发一个 iPad 应用程序,我无法隐藏 UIKeyboardAssistantBar,显示在软键盘上方的栏,带有文本预测等。见下图,它显示了完整的键盘,只是为了给出一个参考 - 我要隐藏的栏位于键盘上方(显示“2”的栏)

我遇到的问题是使用外部键盘时:文本视图获得焦点时不显示软键盘,但始终显示辅助栏 - 到目前为止我发现的唯一方法是让用户使用最右侧的图标手动隐藏它。

理想情况下,我正在寻找的解决方案是启用或禁用它的全局调用,这样我就不必为每个文本视图单独处理。

有什么想法吗?

【问题讨论】:

标签: ios ipad keyboard


【解决方案1】:

有一个技巧你可以试试。代码如下:

let item = self.yourTextView.inputAssistantItem;
item.leadingBarButtonGroups = [];
item.trailingBarButtonGroups = [];

【讨论】:

  • +1 因为这行得通,但我更喜欢适用于每个文本视图的解决方案 - 如果这样的解决方案曾经存在......
  • 好的,没有其他解决方案...我接受您的解决方案,因为这也是我在我正在处理的项目中解决问题的方式。客户很高兴,我也很高兴:)
【解决方案2】:

公认的解决方案将隐藏键盘上的前导和尾随 BarButtonGroups,但不幸的是它不会隐藏建议/自动更正栏(带有建议“2”的中心按钮。

我需要一个 iPad 原生应用程序,它需要一个使用 WKWebView 呈现 HTML 登录页面的 HTML 登录页面。为了隐藏建议按钮,我使用了一些注入的 javascript,因为我无法控制 HTML 登录页面。下面的 Swift 3 代码创建 WKWebView(替换视图对象并将 userScript 注入页面):

    var webView: WKWebView!

override func loadView() {

    let autocorrectJavaScript = "var inputTextElement = document.getElementById('userName');"
    + "   if (inputTextElement != null) {"
    + "     var autocorrectAttribute = document.createAttribute('autocorrect');"
    + "     autocorrectAttribute.value = 'off';"
    + "     inputTextElement.setAttributeNode(autocorrectAttribute);"
    + "   }"
    let userScript = WKUserScript(source: autocorrectJavaScript, injectionTime: .atDocumentEnd, forMainFrameOnly: false)
    let webConfiguration = WKWebViewConfiguration()
    webConfiguration.userContentController.addUserScript(userScript)
    webView = WKWebView(frame: .zero, configuration: webConfiguration)
    view = webView
}

【讨论】:

    【解决方案3】:

    实际上,这是一个有效的方法,即使启用了contenteditable

    func findKeyboardAssistantView() -> UIView? {
        let result: UIView? = nil
    
        let windows = UIApplication.shared.windows
    
        let prefixes = [
            "<UIInputSetContainerView",
            "<UIInputSetHostView",
            "<_UIKBCompatInputView",
            "<UIKeyboardAutomatic",
            "<UIKeyboardImpl",
        ]
        for window in windows {
            if window.description.hasPrefix("<UIRemoteKeyboardWindow") {
                var last = window.subviews
                for p in prefixes {
                    for s in last {
                        if s.description.hasPrefix(p) {
                            last = s.subviews
                        }
                    }
                }
                for s in last {
                    if s.description.hasPrefix("<UIKeyboardAssistantBar") {
                        return s
                    }
                }
                break
            }
        }
        return result
    }
    findKeyboardAssistantView()?.isHidden = true
    

    注意必须在发送UIResponder.keyboardWillShowNotification 时触发

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-12
      • 2016-02-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多