ios - 如何在容器视图中显示 WKWebView 内容
问题描述
以下代码正在运行,但问题是此视图位于导航中,因此 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
}
}
解决方案
设置框架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),
])
推荐阅读
- django - 如何在 Django 我们应用程序中为 mongoDB 连接配置 settings.py
- asp.net-core - 如何防止通过 IHttpClientFactory 的 HttpClient 登录到 ILogger?
- sql-server - SQL Server 2008 R2 - 在 Lag / CTE 中考虑前一天 - 使用上一行或下一行的值
- swift - 每秒调用多次的函数中的延迟代码
- python - 在 for 循环中从列表中解压缩多个参数
- c# - 上传文件从邮递员那里工作,但不是通过代码
- scala - 可以在 spark 中处理多字符分隔符
- javascript - 使用 node.js 检查 mysql 表
- c# - 每个具有多个 azure 功能的解决方案的通用配置文件问题
- javascript - D3 交互式图例和线条