美文网首页
Swift 修改WKWebView的keyboardAppear

Swift 修改WKWebView的keyboardAppear

作者: 大吉__ | 来源:发表于2019-04-12 10:15 被阅读0次

最近做个需求,期望修改wkwebview键盘弹起时的键盘颜色为黑色,但是看文档keyboardAppearance只在UITextView相关组件上。

google查到的ionic-plugin-keyboard有一个解决方案,不过是OC的,现翻译为swift的实现

extension WKWebView {

  @objc var darkKeyboardAppearanceTemplateMethod: UIKeyboardAppearance {

    return .dark

  }

  @objc var lightKeyboardAppearanceTemplateMethod: UIKeyboardAppearance {

    return .light

  }

  func setStyleDark (isDark: Bool = true) {

    var candidateView: UIView? = nil

    for view in self.scrollView.subviews {

      if String(describing: type(of: view)).hasPrefix("WKContent") {

        candidateView = view

      }

    }

    guard let targetView = candidateView else {

      return

    }

    var method: Method!

    if isDark {

      method = class_getInstanceMethod(EditorWebView.self, #selector(getter: darkKeyboardAppearanceTemplateMethod))

    } else {

      method = class_getInstanceMethod(EditorWebView.self, #selector(getter: lightKeyboardAppearanceTemplateMethod))

    }

    let imp = method_getImplementation(method!)

    let typeEncoding = method_getTypeEncoding(method!)

    class_replaceMethod(targetView.superclass, #selector(getter: UITextInputTraits.keyboardAppearance), imp, typeEncoding)

  }

}

初始化webview后调用webview.setStyleDark即可

ps: 感觉苹果对webview都是区别对待啊

相关文章

网友评论

      本文标题:Swift 修改WKWebView的keyboardAppear

      本文链接:https://www.haomeiwen.com/subject/dbeqwqtx.html