[英]How to call a UIViewController from SwiftUI
I have a SwiftUI TabView, with the 2nd tab item needing to show a UIViewController.我有一个 SwiftUI TabView,第二个选项卡项需要显示一个 UIViewController。 How can I embed the UIViewController in this second tab?
如何在第二个选项卡中嵌入 UIViewController?
class MyUIVC: UIViewController {.....}
The following is some pseudocode to what I want:以下是我想要的一些伪代码:
struct myTabView: View {
@State var selectedTab: Int = 0
var body: some View {
mySwiftUIView
.tabItem { Text("First Tab") }
.tag(0)
MyUIVC //This is what I want to do with my UIViewController
.tabItem { Text("Second Tab") }
.tag(1)
}
}
You need to bridge the ViewController using UIViewControllerRepresentable.
您需要使用
UIViewControllerRepresentable.
桥接 ViewController UIViewControllerRepresentable.
. . Apple provides documentation on how to use that here:
Apple 在此处提供了有关如何使用它的文档:
https://developer.apple.com/documentation/swiftui/uiviewcontrollerrepresentable https://developer.apple.com/documentation/swiftui/uiviewcontrollerrepresentable
Additionally, there's a working example of it within their SwiftUI lessons, most notably the lesson titled "Interfacing with UIKit" which can be found here:此外,在他们的 SwiftUI 课程中有一个关于它的工作示例,最著名的是标题为“与 UIKit 交互”的课程,可以在这里找到:
https://developer.apple.com/tutorials/swiftui/interfacing-with-uikit https://developer.apple.com/tutorials/swiftui/interface-with-uikit
The example ViewController that they're working with looks like this:他们正在使用的示例 ViewController 如下所示:
struct PageViewController: UIViewControllerRepresentable {
var controllers: [UIViewController]
func makeUIViewController(context: Context) -> UIPageViewController {
let pageViewController = UIPageViewController(
transitionStyle: .scroll,
navigationOrientation: .horizontal)
return pageViewController
}
func updateUIViewController(_ pageViewController: UIPageViewController, context: Context) {
pageViewController.setViewControllers(
[controllers[0]], direction: .forward, animated: true)
}
class Coordinator: NSObject {
var parent: PageViewController
init(_ pageViewController: PageViewController) {
self.parent = pageViewController
}
}
}
That being said, the heart and soul of bridging a ViewController is:话虽如此,桥接 ViewController 的核心和灵魂是:
1) Create a struct with a unique name for the ViewController that inherits from UIViewControllerRepresentable
1) 为继承自
UIViewControllerRepresentable
的 ViewController 创建一个具有唯一名称的结构
2) Implement the function makeUIViewController(context: Context)
with a return type of the UIViewController 2)实现函数
makeUIViewController(context: Context)
,返回类型为UIViewController
3) Implement the function updateUIViewController(_ uiViewController: YourUIViewController, context: Context)
without a return 3) 实现函数
updateUIViewController(_ uiViewController: YourUIViewController, context: Context)
无返回
4) As needed, implement a Coordinator
class that inherits from NSObject
as well as any delegate protocols you need. 4) 根据需要,实现一个从
NSObject
继承的Coordinator
类以及您需要的任何委托协议。
There is a bit more depending on what you need that ViewController to do, but this should get you pointed in the right direction.还有一点取决于您需要 ViewController 做什么,但这应该让您指向正确的方向。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.