iOS-WKWebView取消自動選中灰色背景

在IOS中WKWebView有些地方tap點擊會有一個灰色背景圖層出現,會讓用戶感覺是個bug.
tap點擊WKWebView會有一個灰色背景圖層出現

-webkit-tap-highlight-color這個屬性只用於iOS (iPhone和iPad)。當你點擊一個鏈接或者通過Javascript定義的可點擊元素的時候,它就會出現一個半透明的灰色背景。要重設這個表現,你可以設置-webkit-tap-highlight-color爲任何顏色。想要禁用這個高亮,設置顏色的alpha值爲0即可。

示例:

//設置高亮色爲50%透明的紅色:
-webkit-tap-highlight-color: rgba(255,0,0,0.5);

//設置高亮色透明
-webkit-tap-highlight-color: rgba(0,0,0,0);

解決方案:

//swift代碼
//去除wkwebview tap選中顏色
//script1 和script2都可以
    func disable_webkit_tap_highlight_color()  {
        //let script1= "function addStyleString(str) {" + "var node = document.createElement('style');" + "node.innerHTML = str;" + "document.body.appendChild(node);" + "}" + "addStyleString('* {-webkit-tap-highlight-color: rgba(0,0,0,0);}');"
        let script2= "document.body.style.webkitTapHighlightColor='transparent';"
        self.webView.evaluateJavaScript(script2, completionHandler: nil)
    }

參考文章-stackoverflow-How can I disable the automatic gray selection in WKWebView?

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章