iosswiftvirtual-keyboard

Swift iOS disable keyboard autocomplete and top part


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


Solution

  • 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 :)