图片不适合 WKWebView
Image not fitting into WKWebView
正在尝试显示以下请求。
class ViewController: UIViewController, WKNavigationDelegate {
@IBOutlet weak var webView: WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
webView.navigationDelegate = self
let htmlString = "<p><a href=\"https://miro.medium.com/max/3336/1*iGyyen2re8-dgQJlL65axw.png\"><img src=\"https://miro.medium.com/max/3336/1*iGyyen2re8-dgQJlL65axw.png\" class=\"alignnone size-full\"></a></p>\n<p></p>\n<p>Vijay Chandrasekhar: \"Robin Uthappa, Faiz Gaza's have already users kookaburra bats before in an international game\"<br><br>Sivan dune just got his t29I cap from Ravi Shastri. He becomes the 82nd cricketer to represent Indian in the format. </p>"
var headerString = "<header><meta name='viewport' content='width=device-width, initial-scale=0.65, maximum-scale=0.65, minimum-scale=0.65'></header>"
headerString.append(htmlString)
webView.loadHTMLString(headerString, baseURL: nil)
}
不幸的是,WKWebview 中显示的图像非常大。因此,水平滚动发生了。但是,我不想要这种行为。
期望图像大小应该与 WKWebView 相同。
您可以考虑以下两种解决方案之一:
- 只需使用 CSS
width: 100%
拉伸即可(高度将自动计算)
- 将其用作整个页面的背景图像并使用 CSS
background-size: 100%
或 background-size: cover
拉伸它
实际看到这个:
Stretch and scale a CSS image in the background - with CSS only
正在尝试显示以下请求。
class ViewController: UIViewController, WKNavigationDelegate {
@IBOutlet weak var webView: WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
webView.navigationDelegate = self
let htmlString = "<p><a href=\"https://miro.medium.com/max/3336/1*iGyyen2re8-dgQJlL65axw.png\"><img src=\"https://miro.medium.com/max/3336/1*iGyyen2re8-dgQJlL65axw.png\" class=\"alignnone size-full\"></a></p>\n<p></p>\n<p>Vijay Chandrasekhar: \"Robin Uthappa, Faiz Gaza's have already users kookaburra bats before in an international game\"<br><br>Sivan dune just got his t29I cap from Ravi Shastri. He becomes the 82nd cricketer to represent Indian in the format. </p>"
var headerString = "<header><meta name='viewport' content='width=device-width, initial-scale=0.65, maximum-scale=0.65, minimum-scale=0.65'></header>"
headerString.append(htmlString)
webView.loadHTMLString(headerString, baseURL: nil)
}
不幸的是,WKWebview 中显示的图像非常大。因此,水平滚动发生了。但是,我不想要这种行为。
期望图像大小应该与 WKWebView 相同。
您可以考虑以下两种解决方案之一:
- 只需使用 CSS
width: 100%
拉伸即可(高度将自动计算) - 将其用作整个页面的背景图像并使用 CSS
background-size: 100%
或background-size: cover
拉伸它
实际看到这个: Stretch and scale a CSS image in the background - with CSS only