refresh scroll view from child view in swiftui in swift

You can refresh the ScrollView from the child view by passing the parent's reference to the child view and updating the state of the parent from the child view.

Here's an example of how you can do this:

main.swift
struct ParentView: View {
    @State private var refreshScrollView = false
    
    var body: some View {
        ScrollView {
            ChildView(refreshScrollView: $refreshScrollView)
        }
    }
}

struct ChildView: View {
    @Binding var refreshScrollView: Bool
    
    var body: some View {
        Button(action: {
            // Update the refreshScrollView binding to trigger a refresh of the ScrollView
            self.refreshScrollView.toggle()
        }) {
            Text("Refresh ScrollView")
        }
    }
}
534 chars
23 lines

In the example above, the ParentView has a @State variable called "refreshScrollView" that is passed as a binding to the ChildView. The ChildView contains a button that toggles the refreshScrollView binding when pressed. Toggling this binding state will update the ParentView's state causing the ScrollView to refresh.

related categories

gistlibby LogSnag