Not really sure what your scenario is, so there might be a simpler way.
However, I had a somewhat similar situation, but this was without a ScrollView. I found something on Hacking With Swift forum that got me to a solution (bit of a hack)
Create a State variable that stores the CGSize of your content
Put an overlay on your content view that has a GeometryReader with a Color.Clear view (this is the tacky part)
In the onAppear of your Color.Clear, set the CGSize State to your GeometryProxy's size
Use a basic .frame modifier to set the size of your Scrollview to the size of the State CGSize
struct ContentView: View {
@State private var contentSize: CGSize = .zero
var body: some View {
ScrollView {
Rectangle()
.fill(Color.blue)
.frame(width: 100, height: 100)
.overlay(
GeometryReader { geo in
Color.clear.onAppear {
contentSize = geo.size
}
}
)
}
.background(Color.red)
.frame(maxWidth: contentSize.width, maxHeight: contentSize.height)
}
}
Topic:
UI Frameworks
SubTopic:
SwiftUI
Tags: