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…

MING
Tech Blog Today

--

Image: developer.apple.com

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
WWDC20

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
#endif
var body: some View {
#if os(iOS)
if horizontalSizeClass…

--

--

MING
Tech Blog Today

 WWDC Award Winner | Project Manager, previous Senior Software Engineer, & UI Designer | Contact: hi@1998.media | Website: 1998.media