{"id":20529661,"url":"https://github.com/amzd/scrollviewproxy","last_synced_at":"2025-05-08T23:43:28.794Z","repository":{"id":55917856,"uuid":"269026830","full_name":"Amzd/ScrollViewProxy","owner":"Amzd","description":"ScrollViewProxy for SwiftUI on iOS 13 and up","archived":false,"fork":false,"pushed_at":"2023-04-24T06:00:21.000Z","size":70,"stargazers_count":172,"open_issues_count":4,"forks_count":32,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-05-08T23:43:20.755Z","etag":null,"topics":["apple","ios","macos","scrollview","swift","swift-package-manager","swiftui"],"latest_commit_sha":null,"homepage":"","language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Amzd.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":["Amzd"],"patreon":null,"open_collective":null,"ko_fi":null,"tidelift":null,"community_bridge":null,"liberapay":null,"issuehunt":null,"otechie":null,"custom":null}},"created_at":"2020-06-03T08:02:49.000Z","updated_at":"2024-10-22T04:54:13.000Z","dependencies_parsed_at":"2024-06-21T17:51:06.267Z","dependency_job_id":"d0476693-45ba-45ce-b9ae-57e88ca94958","html_url":"https://github.com/Amzd/ScrollViewProxy","commit_stats":{"total_commits":39,"total_committers":3,"mean_commits":13.0,"dds":"0.28205128205128205","last_synced_commit":"c34d2950c0feb7ab8a044aa170bb090e60c7cdea"},"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Amzd%2FScrollViewProxy","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Amzd%2FScrollViewProxy/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Amzd%2FScrollViewProxy/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Amzd%2FScrollViewProxy/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Amzd","download_url":"https://codeload.github.com/Amzd/ScrollViewProxy/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253166474,"owners_count":21864467,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["apple","ios","macos","scrollview","swift","swift-package-manager","swiftui"],"created_at":"2024-11-15T23:33:29.499Z","updated_at":"2025-05-08T23:43:28.750Z","avatar_url":"https://github.com/Amzd.png","language":"Swift","readme":"As of June 22 2020 this is included in the SwiftUI 2 beta. [https://developer.apple.com/documentation/swiftui/scrollviewproxy](https://developer.apple.com/documentation/swiftui/scrollviewproxy)\n\nThe Apple implementation uses just `.id(_:)` and I had update issues with that where Views with an id sometimes won't update when their ObservedObject changed. Maybe this has been fixed in the new SwiftUI 2 beta.\n\nAlso the Apple implementation only supports iOS 14 so I think this repo is still useful for backwards compatibility.\n\n**Note:** An important difference between this library and Apples implementation is that the ScrollViewReader goes *inside* the ScrollView. If you place the ScrollViewReader around the ScrollView the scrollTo function will not scroll to the correct location (due to its coordinateSpace not being part of the scrolling content). I considered fixing this to align with Apple but that would break backwards compatibility with projects already using ScrollViewProxy.\n\nMaybe it's possible to detect if we're inside or outside of a ScrollView in the reader but then how do we handle nested ScrollViews? If you have any ideas please open an Issue/PR or email me.\n\n# ScrollViewProxy\n\nAdds `ScrollViewReader` and `ScrollViewProxy` that help you scroll to locations in a ScrollView\n\n\nTo get a ScrollViewProxy you can either use the conveinience init on ScrollView\n\n```swift\nScrollView { proxy in\n    ...\n}\n```\n\nor add a ScrollViewReader to any View that creates a UIScrollView under the hood\n\n```swift\nList {\n    ScrollViewReader { proxy in\n        ...\n    }\n}\n```\n\nThe ScrollViewProxy currently has one variable and two functions you can call\n\n```swift\n/// A publisher that publishes changes to the scroll views offset\npublic var offset: OffsetPublisher\n\n/// Scrolls to an edge or corner\npublic func scrollTo(_ alignment: Alignment, animated: Bool = true)\n\n/// Scrolls the view with ID to an edge or corner\npublic func scrollTo(_ id: ID, alignment: Alignment = .top, animated: Bool = true)\n```\n\nTo use the scroll to ID function you have to add an ID to the view you want to scroll to\n\n```swift\nScrollView { proxy in\n    HStack { ... }\n        .scrollId(\"someId\")\n}\n```\n*This is the only part that is different from the SwiftUI 2.0 implementation because I don't know how to access Views by ID from the `.id(_:)` function*\n\n## Example\n\nEverything put together in an example\n\n```swift\nstruct ScrollViewProxyExample: View {\n    \n    @State var randomInt = Int.random(in: 0..\u003c200)\n    @State var proxy: ScrollViewProxy? = nil\n    @State var offset: CGPoint = .zero\n    \n    var body: some View {\n        // GeometryReader for safeAreaInsets on Sticky View\n        GeometryReader { geometry in \n            VStack {\n                ScrollView { proxy in\n                    Text(\"Sticky View\")\n                        .background(Color.white)\n                        .onReceive(proxy.offset) { self.offset = $0 }\n                        .offset(x: offset.x, y: offset.y + geometry.safeAreaInsets.top)\n                        .zIndex(1)\n                    \n                    VStack(spacing: 20) {\n                        ForEach(0..\u003c200) { index in\n                            HStack {\n                                Spacer()\n                                Text(\"Item: \\(index)\").font(.title)\n                                Spacer()\n                            }.scrollId(index)\n                        }\n                    }\n                    .zIndex(0)\n                    .onAppear {\n                        self.proxy = proxy\n                    }\n                }\n                HStack {\n                    Button(action: {\n                        self.proxy?.scrollTo(self.randomInt, alignment: .center)\n                        self.randomInt = Int.random(in: 0..\u003c200)\n                    }, label: {\n                        Text(\"Go to \\(self.randomInt)\")\n                    })\n                    Spacer()\n                    Button(action: { self.proxy?.scrollTo(.bottom) }, label: {\n                        Text(\"Bottom\")\n                    })\n                    Spacer()\n                    Button(action: { self.proxy?.scrollTo(.center) }, label: {\n                        Text(\"Center\")\n                    })\n                }.padding()\n            }\n        }\n    }\n}\n```\n\n## Deintegrate\n\nWant to drop iOS 13 support and move to the SwiftUI 2.0 version?\n\n1. Remove the Package\n2. Add this extension:\n\n```swift\nextension View {\n    public func scrollId\u003cID: Hashable\u003e(_ id: ID) -\u003e some View {\n        id(id)\n    } \n}\n```\n\n(Or replace all .scrollId with .id)\n","funding_links":["https://github.com/sponsors/Amzd"],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Famzd%2Fscrollviewproxy","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Famzd%2Fscrollviewproxy","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Famzd%2Fscrollviewproxy/lists"}