uikit2 uikit3
SwiftUI was introduced in iOS 13 at a time when many of us have a big app built with UIKit. SwiftUI makes us enthusiastic to get started with this new framework for building apps, just like when Swift was first introduced.
在我们许多人都使用UIKit构建大型应用程序的时候,iOS 13中引入了SwiftUI。 就像首次引入Swift一样,SwiftUI使我们热衷于开始使用这个用于构建应用程序的新框架。
When Swift was introduced we had to migrate from Objective-C to Swift. Many apps are probably still using (partly) Objective-C, as that transition can take quite some time. It’s good to know how to start the transition to make this process as short as possible. iOS 14 is getting close and you might consider dropping iOS 12 later this year, allowing you to write new views in SwiftUI. So, here’s an article explaining how you can use SwiftUI within a UIKit application.
引入Swift时,我们不得不从Objective-C迁移到Swift。 许多应用可能仍在使用(部分)Objective-C,因为这种转换可能会花费一些时间。 很高兴知道如何开始过渡以使此过程尽可能短。 iOS 14已经接近尾声,您可以考虑在今年晚些时候删除iOS 12 ,以便您在SwiftUI中编写新视图。 因此,本文介绍了如何在UIKit应用程序中使用SwiftUI。
Adding SwiftUI views in a UIKit application early on will make your future self a lot happier!
尽早在UIKit应用程序中添加SwiftUI视图将使您未来的生活更快乐!
(The Benefits of Using SwiftUI As Soon as Possible in a UIKit App)
Transitioning from a UIKit app to SwiftUI is time-consuming — for larger apps it will probably take years. Apps written in Objective-C had to make the same transition when Swift was introduced and are probably still not completely moved over. So, adopting SwiftUI early comes with a few benefits:
从UIKit应用程序过渡到SwiftUI非常耗时-对于大型应用程序可能需要数年时间。 在引入Swift时,用Objective-C编写的应用程序必须进行相同的转换,并且可能仍未完全移开。 因此,尽早采用SwiftUI有一些好处:
- There’s no need to rewrite your new views in the future to SwiftUI.
- You start learning SwiftUI while developing as you normally do.
- The transition to SwiftUI takes time. The earlier you start, the earlier your app will be completely written in SwiftUI.
(Things to Consider When Adopting SwiftUI in a UIKit Application)
It’s important to know that you can only use SwiftUI in iOS 13 and up. On top of that, SwiftUI is a new technology that’s been improved in later updates. Dropping iOS 12 means you can start with SwiftUI but it also means that you start using the first version of SwiftUI. This could lead to unexpected behavior and bugs from the early days.
重要的是要知道您只能在iOS 13及更高版本中使用SwiftUI。 最重要的是,SwiftUI是一项新技术,在以后的更新中得到了改进。 删除iOS 12意味着您可以从SwiftUI开始,但这也意味着您开始使用SwiftUI的第一个版本。 从早期开始,这可能会导致意外行为和错误。
(Using SwiftUI for new features only)
You could decide to use SwiftUI for new features only. This is possible even if you’re not dropping iOS 12. You will use the available APIs which makes your newly written views available only on the versions that support SwiftUI.
您可以决定仅将SwiftUI用于新功能。 即使您没有删除iOS 12,这也是可能的。您将使用可用的API,这些API使新编写的视图仅在支持SwiftUI的版本上可用。
if #available(iOS 13.0, *) {
presentSwiftUIView()
} else {
// Fallback on earlier versions
}
You can decide to deliver new features with SwiftUI, which future-proofs your code as it removes the need to rewrite it in a few years. This obviously only works if you can decide to make a certain feature only available to users on iOS 13 and up.
您可以决定使用SwiftUI提供新功能,因为它可以在未来几年内对代码进行过时验证,因为它无需在几年内重写代码。 很明显,这只有在您可以决定使某些功能仅对iOS 13及更高版本的用户可用时才有效。
(Presenting a SwiftUI View in a UIKit View Controller)
Once you have a feature you’d like to develop with SwiftUI it’s time to integrate it using the SwiftUI framework. SwiftUI introduced an instance called UIHostingController
that takes the responsibility of "hosting" a SwiftUI view.
一旦拥有了要使用SwiftUI开发的功能,就可以使用SwiftUI框架进行集成了。 SwiftUI引入了一个名为UIHostingController
的实例,该实例负责“托管” SwiftUI视图。
UIHostingController
itself inherits from UIViewController
which makes it possible to work with it just as you would with any other view controller instance. So, the code to present a SwiftUI view is as simple as this:
UIHostingController
本身继承自UIViewController
,这使其可以像处理任何其他视图控制器实例一样使用它。 因此,呈现SwiftUI视图的代码很简单:
func presentSwiftUIView() {
let swiftUIView = SwiftUIView()
let hostingController = UIHostingController(rootView: swiftUIView)
present(hostingController, animated: true, completion: nil)
}
(Adding a SwiftUI View to a UIKit View)
The same technique applies to adding a SwiftUI view to a UIKit view hierarchy. The only difference is you’re not presenting the view but adding it as a child view controller:
相同的技术适用于将SwiftUI视图添加到UIKit视图层次结构。 唯一的区别是您不显示视图,而是将其添加为子视图控制器:
There are a few things that we do here:
我们在这里做了几件事:
- First, we add the hosting controller as a child to the current view controller.
- The view is added to the view hierarchy of the current view controller.
- Constraints are set up in code to update the boundaries of our SwiftUI view. You can learn more about writing Auto Layout in code here.
在代码中设置了约束,以更新SwiftUI视图的边界。 您可以在此处了解有关在代码中编写自动布局的更多信息。 - The hosting controller is notified that its containing SwiftUI view has moved to a parent.
(Creating an Extension to Simplify Adding SwiftUI Views)
To make this more efficient we can wrap this logic inside an extension on UIViewController
. This allows us to write the same code above as follows:
为了提高效率,我们可以将此逻辑包装在UIViewController
的扩展中。 这使我们可以编写与上面相同的代码,如下所示:
func addSwiftUIView() {
let swiftUIView = SwiftUIView()
addSubSwiftUIView(swiftUIView, to: view)
}
The extension for this looks mostly the same as our code before:
扩展名与我们之前的代码大致相同:
This makes it really easy to add views to your UIKit view hierarchy and even allows you to add SwiftUI views to container views defined in the interface builder.
这使得将视图添加到UIKit视图层次结构非常容易,甚至允许您将SwiftUI视图添加到在界面构建器中定义的容器视图。
(Conclusion)
Adding SwiftUI views in a UIKit application early will make your future self a lot happier as you won’t have to rewrite it later. With a simple UIViewController
extension method, you can easily add a view with just a few lines of code. Decide whether or not you can build a new feature with SwiftUI and make use of the UIHostingController
to present SwiftUI views.
尽早在UIKit应用程序中添加SwiftUI视图将使您以后的生活更快乐,因为您以后不必重写它。 使用简单的UIViewController
扩展方法,只需几行代码即可轻松添加视图。 决定是否可以使用SwiftUI构建新功能,并利用UIHostingController
呈现SwiftUI视图。
If you like to improve your SwiftUI knowledge further, check out the SwiftUI category page.
如果您想进一步提高SwiftUI的知识,请查看SwiftUI类别页面 。