So when SwiftUI first came out you jumped right into it. You couldn't wait to leave storyboards behind. You learned everything there is to know about state variables and binding. Unfortunately you landed on a project that still uses UIKit. What do you do? Forget everything you have learned and go back to constraints and auto layout? No! Let's see how we can interface UIKit and SwiftUI.
因此,当SwiftUI首次发布时,您就可以直接进入其中。 您迫不及待地将故事板留在后面。 您了解了关于状态变量和绑定的所有知识。 不幸的是,您进入了仍使用UIKit的项目。 你是做什么? 忘记所学的一切,回到约束和自动布局? 没有! 让我们看看如何连接UIKit和SwiftUI。
UIHostingController在这里可以为您提供帮助 (UIHostingController is here to help you)
The component that allows us to do this integration is called UIHostingController. Apple docs states:
允许我们执行此集成的组件称为UIHostingController 。 苹果文档指出:
Create a
UIHostingController
object when you want to integrate SwiftUI views into a UIKit view hierarchy. At creation time, specify the SwiftUI view you want to use as the root view for this view controller; you can change that view later using therootView
property. Use the hosting controller like you would any other view controller, by presenting it or embedding it as a child view controller in your interface.当您想将SwiftUI视图集成到UIKit视图层次结构中时,创建一个
UIHostingController
对象。 在创建时,指定要用作此视图控制器的根视图的SwiftUI视图; 您可以稍后使用rootView
属性更改该视图。 通过将其呈现为子视图控制器或将其嵌入到界面中,可以像使用其他任何视图控制器一样使用托管控制器。
In the sample project we have a simple table view made using UIKit that presents a simple list of movies. When tapping a movie we want to present a SwiftUI detail screen showing its cast and genres. We also want to be able to favorite the movie in this detail screen. We are going to do just that on our table view's tableView:didSelectRowAt: method. See the sample below which was taken from the test project.
在示例项目中,我们使用UIKit创建了一个简单的表视图,该视图显示了一个简单的电影列表。 轻按影片时,我们要显示一个SwiftUI详细信息屏幕,显示其演员和体裁。 我们还希望能够在此详细信息屏幕中收藏电影。 我们将只在表视图的tableView:didSelectRowAt:方法上执行此操作。 请参见下面的示例,该示例来自测试项目。
In the code above movieDetail is our SwiftUI view. All we need to do is instantiate it and pass it as the root view of our UIHostingController. We are also adding to it the movie we want to display in the detail view as an environment object. Pretty simple, uh?
在movieDetail上面的代码中是我们的SwiftUI视图。 我们需要做的就是实例化它,并将其作为UIHostingController的根视图传递 。 我们还向其中添加了要在详细视图中显示的电影作为环境对象。 很简单,嗯?
SwiftUI到UIKit (SwiftUI to UIKit)
What about comunicating back to our UIKit table view controller? If you are paying close attention you may have noticed in the code above that we are also setting a delegate and an index path of our MovieDetail class. We will use those to reload the movie we have favorited on our movies list.
与我们的UIKit表视图控制器通信该怎么办? 如果您一直在密切注意,您可能已经在上面的代码中注意到我们也在设置MovieDetail类的委托和索引路径。 我们将使用它们来重新加载我们在电影列表中收藏的电影。
This is the code for the favorite button in our MovieDetail swiftUI class. When pressed it will toggle the movie favorite status and also call the didSetFavorite method of our delegate to update the appropriate row on our movie list. Here is the implementation of that method:
这是MovieDetail swiftUI类中“收藏夹”按钮的代码。 当按下它时,将切换电影收藏状态,并调用我们的委托的didSetFavorite方法以更新电影列表中的相应行。 这是该方法的实现:
That's pretty much it. Here we took a quick look on how to use SwiftUI from UIKit and how to comunicate back and forth between them. The full project can be downloaded at https://github.com/mateus-kobe/uikit-swiftui.
就是这样。 在这里,我们快速了解了如何使用UIKit中的SwiftUI以及如何在它们之间来回通信。 完整的项目可以从https://github.com/mateus-kobe/uikit-swiftui下载。
翻译自: https://medium.com/@mateuskamoei/using-swiftui-from-uikit-5057b32365d