0

Im currently creating an app based out of a WKWebView and every time a keyboard pops up a toolbar above it shows like this: Picture of keyboard with done bar

I would like to remove this toolbar when the keyboard expands.

I have searched stack overflow and not found anything current about this topic (I've only found UIWebView), but if you find a post I missed I will be very thankful.

Im using swift.

All help is appreciated, thank you.

Agoose Banwatti
  • 410
  • 2
  • 13

1 Answers1

1

Found the answer on this post

Credit to Pavel above.

Removing WKWebView Accesory bar in Swift

Add this code to your view controller:

fileprivate final class InputAccessoryHackHelper: NSObject {
    @objc var inputAccessoryView: AnyObject? { return nil }
}

extension WKWebView {
    func hack_removeInputAccessory() {
        print("s")
        guard let target = scrollView.subviews.first(where: {
            String(describing: type(of: $0)).hasPrefix("WKContent")
        }), let superclass = target.superclass else {
            return
        }

        let noInputAccessoryViewClassName = "\(superclass)_NoInputAccessoryView"
        var newClass: AnyClass? = NSClassFromString(noInputAccessoryViewClassName)

        if newClass == nil, let targetClass = object_getClass(target), let classNameCString = noInputAccessoryViewClassName.cString(using: .ascii) {
            newClass = objc_allocateClassPair(targetClass, classNameCString, 0)

            if let newClass = newClass {
                objc_registerClassPair(newClass)
            }
        }

        guard let noInputAccessoryClass = newClass, let originalMethod = class_getInstanceMethod(InputAccessoryHackHelper.self, #selector(getter: InputAccessoryHackHelper.inputAccessoryView)) else {
            return
        }
        class_addMethod(noInputAccessoryClass.self, #selector(getter: InputAccessoryHackHelper.inputAccessoryView), method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod))
        object_setClass(target, noInputAccessoryClass)
    }
}

And then call it with

        webView.hack_removeInputAccessory()

After loading your view.

Agoose Banwatti
  • 410
  • 2
  • 13