Moh*_*d S 2 uinavigationcontroller ios swift swiftui
我有一个从场景委托实例化的SwiftUI View,UINavigationController
我想UIViewController从SwiftUI推送到另一个 View
My Scene Delegate 显示了 myView是如何实例化的:
let vc = UIHostingController(rootView:SwiftUIView())
let navigationControll = UINavigationController(rootViewController: vc)
self.window?.rootViewController = navigationControll
// Present window to screen
self.window?.makeKeyAndVisible()
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?
您必须将您的产品包装 UIViewController在 SwiftUI 中View,然后导航到它。
例如
struct MyMasterViewController: UIViewControllerRepresentable {
func makeUIViewController(context: Context) -> MasterViewController {
var sb = UIStoryboard(name: "Main", bundle: nil)
var vc = sb.instantiateViewController(identifier: "MasterViewController") as! MasterViewController
return vc
}
func updateUIViewController(_ uiViewController: MasterViewController, context: Context) {
}
typealias UIViewControllerType = MasterViewController
}
Run Code Online (Sandbox Code Playgroud)
并在NavigationLinkSwiftUI中像这样简单地调用它View
struct SwiftUIView: View {
var body: some View {
NavigationLink(destination: MyMasterViewController()) {
Text("Show Detail View")
}.navigationBarTitle("Navigation")
}
}
struct SwiftUIView_Previews: PreviewProvider {
static var previews: some View {
SwiftUIView()
}.
}
Run Code Online (Sandbox Code Playgroud)
在此处阅读有关在 SwiftUI 中包装 ViewControllers 的更多信息
| 归档时间: |
|
| 查看次数: |
1996 次 |
| 最近记录: |