I'm trying to build out a UIViewRepresentable that controls a UISheetPresentationController since the SwiftUI implementation is very limited. I'm currently stuck on getting the nested view within the sheet to update properly when state changes in SwiftUI. I've put together a very simple demo below.
The bug happens when pressing the toggle button. The state updates but the view does not update. Any help is greatly appreciated!
struct ContentView: View {
@State private var toggle = false
var body: some View {
CustomParentView {
Button {
toggle.toggle()
} label: {
Text(toggle.description)
}
}
}
}
struct CustomParentView<Content: View>: UIViewRepresentable {
let content: Content
@inlinable init(@ViewBuilder content: () -> Content) {
self.content = content()
}
func makeUIView(context: Context) -> UIView {
let view = UIView()
let hostingController = UIHostingController(rootView: content)
hostingController.view.frame = view.bounds
hostingController.view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
view.addSubview(hostingController.view)
return view
}
func updateUIView(_ uiView: UIView, context: Context) {
// What do I do here to update content when it updates in SwiftUI?
}
}