Differences between SwiftUI in Monterey and Sequoia

Having written a SwiftUI based app on macOS 15.2 with Xcode 16.2 I built a version for Monterey (which required backing off some modern uses, like #Preview and others). But the SwiftUI layout, positioning and dimensions, didn't use anything new so,

I was surprised that the app did not look exactly the same on Monterey. It was not wildly different, but enough that I had to modify several to frame, padding and font parameters in order to restore my desired appearance.

I'm assuming that over the course of generations of SwiftUI, things change -- that's life, but this was a frustrating process for at least two reasons:

  • view modifiers can't be conditionally compiled so something like this, for example, isn't possible

        RoundedRectangle(cornerRadius: 1)
    <if Monterey>
            .padding(.horizontal, -2.0)
    <else>      
            .padding(.horizontal, -4.0)
    <endif>
            .frame(height: 4.0)
    
  • previewing views is a challenge. Previewing on Sequoia doesn't show what the views will look like on Monterey. I can build the app on Monterey and use the non-macro #preview mechanism, but in order to do that, and the above, I'm pushed to maintain two versions of my app because the following doesn't compile:

    #if swift(>=5.9)
    #Preview("Monitor") { MonitorView() }
    #else
    struct MonitorView_Previews: PreviewProvider {
        static var previews: some View { 
            MonitorView() 
        }
    }
    #endif
    

These are not show-stoppers but I do wonder if I'm missing some mechanism(s) to ease the pain, or if SwiftUI functional changes are documented.

Yes, the syntax you are looking for would be convenient…

But you can do it a bit differently.

Like this

    func conditionalRect() -> some View {
        if #available(macOS 16.0, *) {
            return RoundedRectangle(cornerRadius: 1)
                .padding(.horizontal, -2.0)
                .frame(height: 4.0)
        } else {
            return RoundedRectangle(cornerRadius: 1)
                .padding(.horizontal, -4.0)
                .frame(height: 4.0)  
         }
    }

Or more simply, create a computed var

var horizPadding : CGFloat {
        if #available(macOS 16.0, *) {
            return -2.0
        } else {
            return -4.0  
        }
}

Then call

    RoundedRectangle(cornerRadius: 1)
        .padding(.horizontal, horizPadding)
        .frame(height: 4.0)
Differences between SwiftUI in Monterey and Sequoia
 
 
Q