'How to determine if a popover will be displayed as a popover or sheet in SwiftUI?

In SwiftUI when a popover is displayed, it will display as either a popover or sheet depending on the device (iPad or iPhone) and window space available.

Is there a correct heuristic to check if the popover will be displayed as a popover or a sheet?

For example, on iPad, a popover will show as a sheet when multitasking and vertical or when horizontal at quarter-screen size.



Solution 1:[1]

Based on my testing you CAN use @Environment(\.horizontalSizeClass) to find out:

struct ContentView: View {
    
    @Environment(\.horizontalSizeClass) var sizeClass
    
    @State private var showPopover = false
    
    var body: some View {
        Button("Show PopOver") {
            showPopover = true
        }
        .popover(isPresented: $showPopover) {
            Text(sizeClass == .regular ? "regular size" : "compact size")
                .frame(width: 300, height: 300)
        }
    }
}

enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 ChrisR