Reputation: 7922
I'm developing a multi platform app that supports: iPhone (portrait), iPad (portrait and landscape) and Mac, in the main ContentView
I want to display a TabView
for iPhones, and a NavigationSplitView
for other platforms (iPad and Mac), is the following check correct to do that:
struct ContentView: View {
@Environment(\.horizontalSizeClass) var hSizeClass
@Environment(\.verticalSizeClass) var vSizeClass
@State private var path = NavigationPath()
@State private var selection: Panel? = .home
var body: some View {
Group {
if hSizeClass == .compact && vSizeClass == .regular {
TabView {
NavigationStack(path: $path) {
HomeView()
.tabItem {
Label("Home", systemImage: "home")
}
}
NavigationStack(path: $path) {
SettingsView()
.tabItem {
Label("Settings", systemImage: "gear")
}
}
}
}
else {
NavigationSplitView {
Sidebar(selection: $selection)
} detail: {
NavigationStack(path: $path) {
// shows view depending on selection
}
}
}
}
}
}
Upvotes: 0
Views: 331
Reputation: 84
Your approach can work but will not specifically check for the platform that you are on. To check the difference between iPhone or iPad you can use UIDevice.current.userInterfaceIdiom
.
UIDevice.current.userInterfaceIdiom == .pad // True for ipads
UIDevice.current.userInterfaceIdiom == .phone // True for iphones
This is explained in the following post: https://stackoverflow.com/a/24059373/19115398
To detect macOS you can use #available
if #available(macOS 12.0, *) { ... } // Enters the if statement if system is on macos (in this case only macos 12 or higher but if you edit this to your minimum version it will always be true)
If you combine this you can check for all platforms and add the behaviour you need for it.
Upvotes: 1