-1

Is there a way in Swift to force the iOS keyboard to not have the top part? By saying the top part I mean the autocomplete and the input field switcher tools that appear at the top.

Some of my views have embedded webViews that run local js and I want the keyboard for the inputs in webView to not have that top part of the keyboard. If it`s not possible to disable these for webView specifically, any other method should be fine as well.

Please take a look at this screenshot to see exactly what part of the keyboard I am talking about.

enter image description here

Saik
  • 993
  • 1
  • 16
  • 40

1 Answers1

2

You can try running the below JS in the webview every time you load the web page

var textFields = document.getElementsByTagName('input');

if (textFields) {
    var i;
    for( i = 0; i < textFields.length; i++) {
        var txtField = textFields[i];
        if(txtField) {
            txtField.setAttribute('autocomplete','off');
            txtField.setAttribute('autocorrect','off');
            txtField.setAttribute('autocapitalize','off');
            txtField.setAttribute('spellcheck','false');
        }
    }
}

Additionally write this code to hide the done button accessory view from keyboard

class CustomWebView: WKWebView {
    var accessoryView: UIView?
    override var inputAccessoryView: UIView? {
        return accessoryView
    }
}

And use CustomWebView in place of WKWebView wherever this functionality is needed.

Let me know if you need any more help.

Happy Coding :)

Prateek Varshney
  • 1,114
  • 2
  • 12
  • 29
  • Prateek So we made some progress, thank you very much. The autocorrect part is gone now but what about the navigation part, the one with up and down arrows, and a Done button. Any way to get rid of that from JS side ? Thanks. – Saik Sep 19 '20 at 17:43
  • 1
    But in that case how would you dismiss the keyboard. The done key dismisses the keyboard so that a user can navigate to other views or press action buttons that are hidden behind the keyboard. In case you want that removed you need to think how to dismiss the keyboard if that bar is gone. – Prateek Varshney Sep 19 '20 at 17:48
  • Well, ideally the return button should be dismissing the keyboard in my situation. – Saik Sep 19 '20 at 17:53
  • Updated my code, can you check if it works for you now for both cases. – Prateek Varshney Sep 19 '20 at 18:08
  • Did not make a difference unfortunately :( – Saik Sep 19 '20 at 20:31
  • Hey Saik have updated my answer and this is working well with my code. Please do upvote and mark this as accepted answer in case this helps you and works for you. Thanks :) – Prateek Varshney Sep 20 '20 at 07:20
  • Perfect. That CustomWebView indeed got rid of the top bar. This is totally usable for me but one more question. The return button on the keyboard still acts like a new line basically. Do you know of any way to make it close the keyboard? Thank you very much for your answer by the way. – Saik Sep 20 '20 at 17:42
  • Have you tried this on latest iOS version? I found after setting autocorrect and autocomplete, the suggestion bar still shows? – Hexise Feb 04 '22 at 22:12
  • @Hexise yes top bar with password suggestion and key sumbol didnot hide – sinan Feb 23 '23 at 09:52