I have found a solution. The idea is to read the size of the scroll view and use it as the minimum size of the view inside.
extension View {
func readSize(onChange: @escaping (CGSize) -> Void) -> some View {
background(
GeometryReader { geometryProxy in
Color.clear
.preference(key: SizePreferenceKey.self, value: geometryProxy.size)
}
)
.onPreferenceChange(SizePreferenceKey.self, perform: onChange)
}
}
struct ExampleView: View {
@State var size: CGSize = .zero
var body: some View {
ScrollView([.horizontal, .vertical]) {
Text("Hello World!")
.frame(minWidth: size.width, minHeight: size.height, alignment: .topLeading)
}
.readSize(onChange: { size = $0 })
}
}
Somewhat ugly, but it works.
Topic:
UI Frameworks
SubTopic:
SwiftUI
Tags: