SwiftUI mix and match
You can definitely mix and match SwiftUI with UIKit/AppKit, which means that you can adopt it gradually. Any SwiftUI hierarchy can be embedded in a view controller, and UIKit views can be retrofitted with SwiftUI support:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// You can easily use SwiftUI view hierarchies in UIKit-based
// UIs, by wrapping them in a UIHostingController:
let vc = UIHostingController(rootView: HeaderView())
// For the opposite direction, using UIViews with SwiftUI - just
// create a wrapper that conforms to UIViewRepresentable, which
// acts just like any other SwiftUI view:
struct ProfileSwiftUIView: UIViewRepresentable {
let user: User
func makeUIView(context: Context) -> ProfileView {
return ProfileView()
}
func updateUIView(_ view: ProfileView, context: Context) {
view.nameLabel.text = user.name
view.imageView.image = user.image
}
}
This post is licensed under CC BY 4.0 by the author.