Eduard Eduard - 1 year ago 176
Swift Question

Removing WKWebView Accesory bar in Swift

I am trying for a few days now to get this converted into Swift without really having much background with it.

This is what I've got so far ... and I have been looking on google not really knowing what to search for in order to be more specific. Can you please shed some light on what I'm doing wrong ? Thanks


I have aded the objective-c tag just so more people that are related to this thread may be able to see it and hopefully get an answer.

enter image description here

Answer Source

Michael Dautermann answer has got everything right, but in order to hide the accessory bar you need to swizzle the method inputAccessoryView() of UIView Class with the inputAccessoryView() of the _NoInputAccessoryView class. I have just added the couple of extra lines to the code which does this job of method swizzling.

First you'll need a fake class to swap with

final class FauxBarHelper: NSObject {
    var inputAccessoryView: AnyObject? { return nil }

Then create this method in your controller class

/// Removes the keyboard accessory view from the web view
/// Source: /
func _removeInputAccessoryView(webView: UIWebView) {
    var targetView: UIView? = nil

    for view in webView.scrollView.subviews {
        if String(describing: type(of: view)).hasPrefix("WKContent") {
            targetView = view

    guard let target = targetView else { return }

    let noInputAccessoryViewClassName = "\(target.superclass!)_NoInputAccessoryView"
    var newClass: AnyClass? = NSClassFromString(noInputAccessoryViewClassName)
    if newClass == nil {
        let targetClass: AnyClass = object_getClass(target)
        newClass = objc_allocateClassPair(targetClass, noInputAccessoryViewClassName.cString(using: String.Encoding.ascii)!, 0)

    let originalMethod = class_getInstanceMethod(FauxBarHelper.self, #selector(getter: FauxBarHelper.inputAccessoryView))
    class_addMethod(newClass!.self, #selector(getter: FauxBarHelper.inputAccessoryView), method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod))
    object_setClass(target, newClass)

HTH ;)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download