Skip to content

Instantly share code, notes, and snippets.

@Drag0ndust
Created August 19, 2021 10:00
Show Gist options
  • Save Drag0ndust/4526e4791016210e79c3865fc486b664 to your computer and use it in GitHub Desktop.
Save Drag0ndust/4526e4791016210e79c3865fc486b664 to your computer and use it in GitHub Desktop.
SwiftUI: Detect if view is displayed on iPhone or iPad
struct DeviceSwitchView<iPhoneView: View, iPadView: View>: View {
@Environment(\.horizontalSizeClass) var sizeClass
var iPhone: iPhoneView
var iPad: iPadView
init(@ViewBuilder iPhone: () -> iPhoneView, @ViewBuilder iPad: () -> iPadView) {
self.iPhone = iPhone()
self.iPad = iPad()
}
var body: some View {
NavigationView {
if sizeClass == .compact {
// iPhone
iPhone
} else {
// iPad
iPad
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment