swiftxcodeswiftuiios13uiactivityviewcontroller

SwiftUI - Half modal?


I'm trying to recreate a Modal just like Safari in iOS13 in SwiftUI:

Here's what it looks like:

enter image description here

Does anyone know if this is possible in SwiftUI? I want to show a small half modal, with the option to drag to fullscreen, just like the sharing sheet.

Any advice is much appreciated!


Solution

  • iOS 16+

    It looks like half sheet is finally supported in iOS 16.

    To manage the size of sheet we can use PresentationDetent and specifically presentationDetents(_:selection:)

    Here's an example from the documentation:

    struct ContentView: View {
        @State private var showSettings = false
        @State private var settingsDetent = PresentationDetent.medium
    
        var body: some View {
            Button("View Settings") {
                showSettings = true
            }
            .sheet(isPresented: $showSettings) {
                SettingsView()
                    .presentationDetents(
                        [.medium, .large],
                        selection: $settingsDetent
                     )
            }
        }
    }
    

    Note that if you provide more that one detent, people can drag the sheet to resize it.

    Here are possible values for PresentationDetent: