Articles, podcasts and news about Swift development, by John Sundell.

SwiftUI mix and match

Published on 05 Jun 2019
Discover page available: SwiftUI

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 retrofitted with SwiftUI support:

// 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
    }
}