Created
August 19, 2021 10:00
-
-
Save Drag0ndust/4526e4791016210e79c3865fc486b664 to your computer and use it in GitHub Desktop.
SwiftUI: Detect if view is displayed on iPhone or iPad
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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