Specific Dynamic Type settings cause Spacer to stop spacing

I've filed FB13900806 for this but I'm looking for workarounds or guidance on better ways to do this.

tl;dr: My view layout breaks when Dynamic Type is set to Medium, AX1, or AX2, because a Spacer() incorrectly collapses to zero height.

Details: I'm trying to build out a vertically scrollable view with the following:

  • A top header view that does not scroll.
  • A scroll view, which contains a variable amount of content that will not always fill the screen
  • A bottom view that is scrollable, but should be bottom aligned if the content doesn't fill the screen.

Here's how it should look:

I'm using the following logic to achieve this, which works for most Dynamic Type sizes:

import SwiftUI

struct CollapsingSpacerView: View {
  var body: some View {
    VStack {
      Text("Non scrollable, top aligned content")
      GeometryReader { geometry in
        ScrollView(.vertical) {
          VStack(spacing: 20) {
            Text("Scrollable, top aligned content")
            Color.red
              .frame(width: 200, height: 200)
            Spacer()
            Text("Scrollable, bottom aligned content")
          }
          .frame(minWidth: geometry.size.width, minHeight: geometry.size.height)
          .border(.blue)
        }
        .border(.green)
      }
    }
  }
}

#Preview("spacer") {
  CollapsingSpacerView()
}

However, when I change the Dynamic Type setting to one of: Medium, AX1, or AX2, the Spacer stops spacing and all of the scrollable content gets compressed around the vertical center, like this:

Is there a better way to get this layout without relying on Spacer? One workaround I've found is to give the non-scrolling header a fixed height, but that's less than ideal and would love to hear other options.

Answered by DTS Engineer in 791124022

@jonmindtrip Custom spacing would require a bit more work on your end using @ScaledMetric](https://developer.apple.com/documentation/swiftui/scaledmetric)

You might need to use the @ScaledMetric property wrapper to adapt the View so it scale automatically according to the Dynamic Type settings.

There's also a SwiftUI tutorial about adapting to font styles, Dynamic Type, and varying string lengths, and generally adapting your view to the space text needs that could be helpful for you:

https://developer.apple.com/tutorials/swiftui-concepts/scaling-views-to-complement-text

@jonmindtrip Custom spacing would require a bit more work on your end using @ScaledMetric](https://developer.apple.com/documentation/swiftui/scaledmetric)

You might need to use the @ScaledMetric property wrapper to adapt the View so it scale automatically according to the Dynamic Type settings.

There's also a SwiftUI tutorial about adapting to font styles, Dynamic Type, and varying string lengths, and generally adapting your view to the space text needs that could be helpful for you:

https://developer.apple.com/tutorials/swiftui-concepts/scaling-views-to-complement-text

Specific Dynamic Type settings cause Spacer to stop spacing
 
 
Q