The best of both worlds – integrating UIKit into SwiftUI
SwiftUI was announced at WWDC 2019 and is only available on iOS 13 devices or later. Due to its relative newness, SwiftUI lacks broad API coverage compared to UIKit. For example, as of April 2020, UICollectionViews
, UITextView
, UIKit APIs for displaying grids, and multiline texts are not available in SwiftUI. There is therefore a need to implement certain UIKit APIs in SwiftUI.
This recipe goes over the required process for integrating UIKit into SwiftUI. We will create a project that wraps instances of UIActivityIndicatorView
to display an indicator in SwiftUI.
Getting ready
Open Xcode and create a SwiftUI project named UIKitToSwiftUI
.
How to do it…
UIKit views are displayed in SwiftUI by using the representable protocol. Follow these steps to implement UIActivityIndicatorView
in SwiftUI.
- Within the Xcode menu, click File | New | File and select SwiftUI view. Name the view
ActivityIndicator...