gpt4 book ai didi

ios - WKWebView ScaleToFit

转载 作者:可可西里 更新时间:2023-11-01 06:22:47 24 4
gpt4 key购买 nike

从 UIWebView 移动到 WKWebView,我不知道如何使用 WKWebView 大小上的 loadHTMLString 函数来制作我正在加载的 HTML 以适应 View 边界。我现在加载了 html,但它超出了我 View 的右侧和底部。如何缩放它以适合我的 View 范围?

最佳答案

首先,在 StoryBoard 中创建一个容器 View ,您将在该 View 下添加 WKWebView 作为一个 subview :

@IBOutlet var container: UIView!

然后导入WebKit并初始化它,稍后添加为容器的 subview 。为了保留边界,您必须根据其父 View 为其赋予约束值。我是这样做的:

        let webView = WKWebView(frame: .zero)
container.addSubview(webView)

webView.translatesAutoresizingMaskIntoConstraints = false
let height = NSLayoutConstraint(item: webView, attribute: .height, relatedBy: .equal, toItem: container, attribute: .height, multiplier: 1, constant: 0)
let width = NSLayoutConstraint(item: webView, attribute: .width, relatedBy: .equal, toItem: container, attribute: .width, multiplier: 1, constant: 0)
let leftConstraint = NSLayoutConstraint(item: webView, attribute: .leftMargin, relatedBy: .equal, toItem: container, attribute: .leftMargin, multiplier: 1, constant: 0)
let rightConstraint = NSLayoutConstraint(item: webView, attribute: .rightMargin, relatedBy: .equal, toItem: container, attribute: .rightMargin, multiplier: 1, constant: 0)
let bottomContraint = NSLayoutConstraint(item: webView, attribute: .bottomMargin, relatedBy: .equal, toItem: container, attribute: .bottomMargin, multiplier: 1, constant: 0)
container.addConstraints([height, width, leftConstraint, rightConstraint, bottomContraint])

let myURL = URL(string: "")
let myRequest = URLRequest(url: myURL!)
webView.load(myRequest)

添加所需的 url,您就完成了。

关于ios - WKWebView ScaleToFit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35568868/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com