最近做个需求,期望修改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都是区别对待啊