JavaScript and CSS in WKWebView in swift. WKWebView透過JavaScript和CSS調整網頁畫面

(WKWebView, css, java script, html)

WKWebView除了透過網址開啟網頁,也可以透過html格式直接開啟
將HTML各式透過loadHTMLString呈現網頁

webview.loadHTMLString(data, baseURL: nil)

但即便設定了webview的autolayout,畫面還是無法滿版

參考調整JS將webview畫面滿版

但畫面還是有跑版的情況,甚至試過設定

感謝 Andy大大 提供透過CSS自救的方式,強制html依照樣式顯示

透過 CSS的Style強制設定
figure的margin與padding為0、最大寬度100%且!important設定更高優先權
img的最大寬度100%且!important設定更高優先權

畫面可以不跑版的完全顯示

--

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Peggy Tsai 

Peggy Tsai 

iOS & Golang 隨手記 www.linkedin.com/in/peggytsaifc 歡迎+Linkedin交流~

More from Medium

How to communicate between Electron and Swift application using Notifications

How to create Checkbox in SwiftUI

Localiz(s)ing your Marketplace apps with Crayons

Setting up Vue Storybook with Vite to build a Fintech UI Library