How to: Build A Cross-Platform App with SwiftUI 2
Let me show you how to build a simple app that works on iOS, iPadOS, and macOS…
Published in
2 min readAug 16, 2020
How to know the device is running iOS / macOS?
We can use:
#if os(iOS) //For iPhone/iPad
…#elseif os(macOS) //For Mac
…#elseif os(watchOS) //For WatchOS
…#endif
If you want to divide iPhone and iPad design, before
var body: some View
add the following code,
@Environment(\.horizontalSizeClass)
private var horizontalSizeClass
and then add,
if horizontalSizeClass == .compact //For iPhone
after
var body: some View
Example:
import SwiftUIstruct ContentView: View {
#if os(iOS)
@Environment(\.horizontalSizeClass)
private var horizontalSizeClass
#endifvar body: some View {
#if os(iOS)
if horizontalSizeClass…