Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

How to use uiviewcontroller and uiviewrepresentable in swiftui?


Asked by Jasmine O’brien on Dec 14, 2021 FAQ



Here is the final result: The process of integrating UIView is almost identical to the one of UIViewController. Namely, the SwiftUI view must conform to the UIViewRepresentable protocol and implement the same set of methods. Here is an example of how we can represent UIActivityIndicatorView in SwiftUI:
Additionally,
To represent UIKit views and view controllers in SwiftUI, you create types that conform to the UIViewRepresentable and UIViewControllerRepresentable protocols. Your custom types create and configure the UIKit types that they represent, while SwiftUI manages their life cycle and updates them when needed.
Furthermore, The process of integrating UIView is almost identical to the one of UIViewController. Namely, the SwiftUI view must conform to the UIViewRepresentable protocol and implement the same set of methods. Here is an example of how we can represent UIActivityIndicatorView in SwiftUI:
Just so,
Editor’s note: If you are new to SwiftUI, you can check out our introductory tutorial. To use a UIKit view in SwiftUI, you can wrap the view with the UIViewRepresentable protocol. Basically, you just need to create a struct in SwiftUI that adopts the protocol to create and manage a UIView object.
Likewise,
Like most new frameworks, however, one drawback is that it doesn’t come with all UI controls which are available in UIKit. For example, you can’t find a SwiftUI counterpart of text view. Thankfully, Apple provided a protocol called UIViewRepresentable that allows you easily wrap a UIView and make it available to your SwiftUI project.