我在 Storyboard Swift App 上工作,想要在 Full-Screen 中呈现 SwiftUI 子视图。我使用 嵌入了 SwiftUI 视图UIHostingController
,如下所示:
let contentView = UIHostingController(rootView: ContentView())
override func viewDidLoad() {
super.viewDidLoad()
addChild(contentView)
view.addSubview(contentView.view)
}
这是我的 SwiftUIContentView()
子视图:
import SwiftUI
import UIKit
struct ContentView: View {
var body: some View {
Text("").fullScreenCover(isPresented: /*@START_MENU_TOKEN@*/.constant(true)/*@END_MENU_TOKEN@*/, content: {
FullScreenView.init()
})
}
}
struct FullScreenView: View{
var body: some View {
NavigationView{
MasterView()
}.navigationViewStyle(DoubleColumnNavigationViewStyle())
}
}
struct MasterView: View {
var body: some View {
Form {
Section(header: Text("HEADER")) {
Section {
NavigationLink(destination: UIKitView()) { Text("NAVLINK TEXT") }
}
}
}
.navigationBarTitle("NAVBAR TEXT")
}
}
struct UIKitView: UIViewControllerRepresentable {
typealias UIViewControllerType = SwipeViewController
func makeUIViewController(context: Context) -> SwipeViewController {
let sb = UIStoryboard(name: "Storyboard", bundle: nil)
let viewController = sb.instantiateViewController(identifier: "vc") as! SwipeViewController
return viewController
}
func updateUIViewController(_ uiViewController: SwipeViewController, context: Context) {
}
}
有更好的方法吗?如您所见,我使用一个空文本字段来添加.fullScreenCover:/
谢谢你的帮助!!