2

I'm working on an iPad app, and I am unable to hide the UIKeyboardAssistantBar, the bar shown above the soft keyboard, with text prediction etc. See picture below, which shows the full keyboard, just to give a reference - the bar I want to hide is above the keyboard (the one displaying "2")

Keyboard + assistant bar

The problem I have is when an external keyboard is used: the soft keyboard is not shown when a text view obtains the focus, but that assistant bar is always shown instead - the only way I've found so far is to let the user manually hide it, using the icon at the far right.

Ideally, the solution I'm looking for is a global call that enables or disables that, so that I don't have to handle that individually for each text view.

Any idea?

Antonio
  • 71,651
  • 11
  • 148
  • 165

3 Answers3

5

There is a trick that you can try. Here is the code:

let item = self.yourTextView.inputAssistantItem;
item.leadingBarButtonGroups = [];
item.trailingBarButtonGroups = [];
Thanh Pham
  • 75
  • 2
  • +1 because that works, but I would prefer a solution that works for every text view - if such solution ever exists... – Antonio Jun 14 '16 at 16:04
  • Ok, no other solutions... I an accepting yours, because it's also how I am fixing the problem in the project I'm working on. The client is happy, so am I :) – Antonio Jun 16 '16 at 21:38
0

The accepted solution will hide the leading and trailing BarButtonGroups on the keyboard, however unfortunately it will not hide the suggestion/auto correct bar (the center buttons with the suggested "2".

My need was for an iPad native app that required an HTML login page using a WKWebView to render the HTML login page. To hide the suggestion buttons, I used some injected javascript because I had no control over the HTML login page. Swift 3 code below creates the WKWebView (replaces the view object and injects the userScript into the page):

    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
}
dferrero
  • 467
  • 6
  • 6
0

Actually, here is a method that works, even with contenteditable enabled:

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

Note that it has to be fired when UIResponder.keyboardWillShowNotification is sent

Arthur Guiot
  • 713
  • 10
  • 25