Weekly Swift articles, podcasts and tips 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
    }
}

Support Swift by Sundell by checking out this sponsor:

Paw
Paw

Paw: A GraphQL and REST API client that lets you test and describe the APIs that you call from your app. Just enter the URL of the API endpoint that you’re looking to call, add any headers, parameters, authentication, or body data. Hit return — and everything is automatically checked for you, from the standard OAuth 2 login to very custom API flows.