以下代码正常工作,但问题在于此视图位于“导航”中,因此html内容落后于导航。我想在安全区域内添加另一个容器视图,然后在此处显示html内容。我尝试添加容器视图并将webView分配给该容器视图,但是由于屏幕显示黑色,所以它不起作用。谁能告诉我我们是否可以做到以及如何做到?
导入UIKit 导入WebKit
class MyViewController: UIViewController, WKUIDelegate, WKNavigationDelegate {
var webView: WKWebView!
var content: String!
override func viewDidLoad() {
let htmlString = "<html><head><meta name='viewport' content='initial-scale=1.0'/></head><body>" + content
+ "</body></head></html>"
webView.loadHTMLString(htmlString, baseURL:nil)
}
override func loadView() {
webView = WKWebView()
webView.navigationDelegate = self
webView.uiDelegate = self
view = webView
}
}
答案 0 :(得分:1)
设置webView
webView.frame = CGRect(/* set x, y, wifth and height here*/)
或使用一些自动布局约束
webView.translatesAutoresizingMaskIntoConstraints = false
self.view.addSubview(webView)
NSLayoutConstraint.activate([
webView.heightAnchor.constraint(equalToConstant: 300),
webView.widthAnchor.constraint(equalTo: self.view.widthAnchor),
webView.centerXAnchor.constraint(equalTo: self.view.centerXAnchor),
webView.centerYAnchor.constraint(equalTo: self.view.centerYAnchor),
])