FB7671513
If you run the code below in a playground you will see that tapping the buttons doesn’t change the UI.
However, if you move the GeometryReader
to the outside of ViewModelView
it will begin working again.
import SwiftUI
import PlaygroundSupport
class ViewModel: ObservableObject {
@Published var count = 0
}
struct ViewModelView<Content: View>: View {
@ObservedObject var viewModel = ViewModel()
let content: (ViewModel) -> Content
init(@ViewBuilder content: @escaping (ViewModel) -> Content) {
self.content = content
}
var body: some View {
// move the geometry reader to here to get it working
// GeometryReader { proxy in
self.content(self.viewModel)
// }
}
}
let v = ViewModelView { viewModel in
GeometryReader { proxy in
HStack {
Button("-") { viewModel.count -= 1 }
Text("\(viewModel.count)")
Button("+") { viewModel.count += 1 }
}
}
}
PlaygroundPage.current.setLiveView(v)
The same happens with ScrollViewReader
.