title: SwiftUI: UserInterfaceSizeClass for Universal (macOS & iOS) Views
body: <p>Attempting to reference the <code>@Environment</code> objects <code>horizontalSizeClass</code> and <code>verticalSizeClass</code> on macOS (native, not Catalyst) results in the following errors:</p>
  <blockquote>
  <p>'horizontalSizeClass' is unavailable in macOS</p>
  </blockquote>
  <blockquote>
  <p>'verticalSizeClass' is unavailable in macOS</p>
  </blockquote>
  <p>I appreciate that these properties aren't really applicable for macOS, but this presents a big barrier to creating SwiftUI Views which work universally (i.e. cross-platform across macOS, iOS, etc.).</p>
  <p>One workaround is to wrap all the size-class-specific code inside conditional compilation, but the result is lots of duplication and redundancy (see below example).</p>
  <p>Isn't there a more efficient way to handle this?</p>
  <p><strong>Example Universal View:</strong></p>
  <pre><code>struct ExampleView: View {
  
      #if !os(macOS)
      @Environment(\.horizontalSizeClass) var horizontalSizeClass
      #endif
  
      private var item1: some View {
          Text(&quot;Example Item 1&quot;)
      }
      private var item2: some View {
          Text(&quot;Example Item 2&quot;)
      }
      private var item3: some View {
          Text(&quot;Example Item 3&quot;)
      }
  
      var body: some View {
          VStack {
              #if !os(macOS)
              if horizontalSizeClass == .compact {
                  VStack {
                      item1
                      item2
                      item3
                  }
              } else {
                  HStack {
                      item1
                      item2
                      item3
                  }
              }
              #else
              HStack {
                  item1
                  item2
                  item3
              }
              #endif
          }
      }
  }
  </code></pre>