Search code examples
viewswiftuipresentviewcontroller

Presenting View Controller in SwiftUI


How to achieve what the following Objective-C code achieves with SwiftUI? I haven't been able to get a firm grasp on the ideas presented.

    [self presentViewController:messageViewController animated:YES completion:nil];

Solution

  • As there is no provided related code, so in pseudo-code it would look like the following

    struct YourParentView: View {
       @State private var presented = false
       var body: some View {
    
          // some other code that activates `presented` state
    
          SomeUIElement()
             .sheet(isPresented: $presented) {
                 YourMessageViewControllerRepresentable()
             }
       }
    }