uikit - 将 SwiftUI 视图与 UIView 一起使用的方法
问题描述
我正在使用使用UIKit
. 它的 API 需要一个 UIView 的引用。
如何在 SwiftUI 中使用这个库?以及如何将 SwiftUI 视图转换为 UIView?
我试过创建一个UIViewRepresentable
这样的:
struct SomeView: UIViewRepresentable {
let contentViewController: UIViewController
init<Content: View>(contentView: Content) {
self.contentViewController = UIHostingController(rootView: contentView)
}
func makeUIView(context: Context) -> UIKitView {
// Whatever I do here doesn't work. Frame is always 0
contentViewController.loadViewIfNeeded()
contentViewController.view.setNeedsDisplay()
contentViewController.view.layoutIfNeeded()
print(contentViewController.view.frame)
let uikitView = UIKitView()
uikitView.show(contentViewController.view)
return popover
}
func updateUIView(_ uiView: UIKitView, context: Context) {
}
}
解决方案
推荐阅读
- sql - 当前日期的 N 个月之间的 Spark 窗口范围
- arrays - 矩阵相乘时的 Array{Float64,2} 问题 Julia
- php - 根据用户角色/功能 Wordpress 从菜单中隐藏 DIV
- python - 通过api创建用户keycloak并分配client-role realm-managment
- ruby-on-rails - SSL 证书请求失败。无法导出 CNAME 记录
- here-api - 在 Here Map 的中心叠加图像(如警告图像)?
- google-apps-script - 无法保存文档 - 文档太大。但它不是
- c++ - 通过模板和 SFINAE 支持多个类接口
- string - golang 将 os.ModePerm 转换为字符串
- scala - 有没有更惯用的方法来简化 leftJoin 的结果?