I am trying to optimize a UI with VStacks and spacers in between. The UI is ideally designed for the iPhone 13 Pro screen size (left). For smaller devices, the intention is that the spacers will shrink in a certain way that the UI still looks appealing.
I tried to achieve this by using frames for the Spacers with minHeight
, idealHeight
and maxHeight
. The intended layout appears on the iPhone 13 Pro, but on a smaller device like the iPhone SE the spacers don't scale down to the minWidth
. How can I fix that?
import SwiftUI
struct ContentView: View {
var body: some View {
VStack(spacing: 0) {
// Top Space
Spacer()
.frame(minHeight: 16, idealHeight: 32, maxHeight: .infinity)
.fixedSize()
// VStack 1
VStack(spacing: 0) {
// Image placeholder
Rectangle()
.fill(Color.red)
.frame(height: 175)
Spacer()
.frame(minHeight: 15, idealHeight: 62, maxHeight: .infinity)
.fixedSize()
Text("Abc")
.frame(height: 100)
}
.background(Color.gray)
// Middle Space
Spacer()
.frame(minHeight: 22, idealHeight: 100, maxHeight: .infinity)
.fixedSize()
// VStack 2
VStack(spacing: 0) {
// Image placeholder
Rectangle()
.fill(Color.red)
.frame(height: 100)
Spacer()
.frame(minHeight: 15, idealHeight: 35, maxHeight: .infinity)
.fixedSize()
// Image placeholder
Rectangle()
.fill(Color.red)
.frame(height: 195)
}
.background(Color.gray)
// Bottom Space
Spacer()
.frame(minHeight: 16, idealHeight: 45, maxHeight: .infinity)
.fixedSize()
}
.edgesIgnoringSafeArea(.all)
}
}