Commit b1e8291
[iOS] Fix CollectionView ScrollOffset not resetting when ItemsSource changes (#34488)
<!-- Please let the below note in for people that find this PR -->
> [!NOTE]
> Are you waiting for the changes in this PR to be merged?
> It would be very helpful if you could [test the resulting
artifacts](https://github.com/dotnet/maui/wiki/Testing-PR-Builds) from
this PR and let us know in a comment if this change resolves your issue.
Thank you!
### Issue Details
- On iOS, when using CollectionView with the Scrolled event, the
VerticalOffset value doesn't reset to 0 when changing the ItemsSource.
While the collection view displays the new items correctly, the reported
scroll position remains at the previous offset value instead of
resetting to the top position.
### Root Cause
- On iOS, UICollectionView.ReloadData() does not reset ContentOffset.
ContentOffset retains its previous value, meaning any subsequent
Scrolled callback—which derives VerticalOffset and delta values directly
from ContentOffset—reports the stale, non-zero offset. As a result,
MAUI's VerticalOffset never resets to 0 after the ItemsSource changes.
### Description of Change
- Added logic in ItemsViewController.cs and ItemsViewController2.cs to
reset CollectionView.ContentOffset to zero only when it’s not already at
the origin. This ensures the scroll position resets correctly when the
ItemsSource changes on iOS/MacCatalyst.
- Before resetting the offset, ResetScrollTracking() is invoked via the
internal IScrollTrackingDelegator interface so that the
scrollViewDidScroll callback computes the delta from zero instead of
using a stale offset.
**Test and UI improvements:**
- Updated the test page (Issue7993.xaml) to add a "ScrollToEnd" button
for reliably scrolling the list, and updated label and button
identifiers for improved test automation.
- Added the ScrollToEndClicked handler in Issue7993.xaml.cs to
programmatically scroll to the end of the list, supporting the new test
flow.
- Removed platform-specific test skips and refactored the test
(Issue7993.cs) to use the new "ScrollToEnd" and "NewItemsSource"
buttons, improving reliability and making the test applicable to
iOS/MacCatalyst.
### Issues Fixed
Fixes #26366
Fixes #33500
### Validated the behaviour in the following platforms
- [ ] Windows
- [ ] Android
- [x] iOS
- [x] Mac
### Output
| Before | After |
|----------|----------|
| <video
src="https://github.com/user-attachments/assets/a374859f-25ce-445f-a53f-6354c2d3f201">
| <video
src="https://github.com/user-attachments/assets/2bdc2dd4-43be-41ef-810c-e4098cba791f">
|1 parent cf41f29 commit b1e8291
8 files changed
Lines changed: 59 additions & 16 deletions
File tree
- src/Controls
- src/Core/Handlers
- Items2/iOS
- Items/iOS
- tests
- TestCases.HostApp/Issues/XFIssue
Lines changed: 7 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
Lines changed: 9 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
427 | 427 | | |
428 | 428 | | |
429 | 429 | | |
| 430 | + | |
| 431 | + | |
| 432 | + | |
| 433 | + | |
| 434 | + | |
| 435 | + | |
| 436 | + | |
| 437 | + | |
| 438 | + | |
430 | 439 | | |
431 | 440 | | |
432 | 441 | | |
| |||
Lines changed: 7 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
9 | 9 | | |
10 | 10 | | |
11 | 11 | | |
12 | | - | |
| 12 | + | |
13 | 13 | | |
14 | 14 | | |
15 | 15 | | |
| |||
26 | 26 | | |
27 | 27 | | |
28 | 28 | | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
29 | 35 | | |
30 | 36 | | |
31 | 37 | | |
| |||
Lines changed: 10 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
6 | 6 | | |
7 | 7 | | |
8 | 8 | | |
| 9 | + | |
9 | 10 | | |
10 | 11 | | |
11 | 12 | | |
| |||
298 | 299 | | |
299 | 300 | | |
300 | 301 | | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
| 308 | + | |
| 309 | + | |
| 310 | + | |
301 | 311 | | |
302 | 312 | | |
303 | 313 | | |
| |||
Lines changed: 7 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
10 | 10 | | |
11 | 11 | | |
12 | 12 | | |
13 | | - | |
| 13 | + | |
14 | 14 | | |
15 | 15 | | |
16 | 16 | | |
| |||
27 | 27 | | |
28 | 28 | | |
29 | 29 | | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
30 | 36 | | |
31 | 37 | | |
32 | 38 | | |
| |||
Lines changed: 5 additions & 4 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
12 | 12 | | |
13 | 13 | | |
14 | 14 | | |
15 | | - | |
16 | | - | |
17 | | - | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
18 | 19 | | |
19 | 20 | | |
20 | | - | |
| 21 | + | |
21 | 22 | | |
22 | 23 | | |
23 | 24 | | |
| |||
Lines changed: 9 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
17 | 17 | | |
18 | 18 | | |
19 | 19 | | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
20 | 29 | | |
21 | 30 | | |
22 | 31 | | |
| |||
Lines changed: 5 additions & 10 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
2 | | - | |
| 1 | + | |
3 | 2 | | |
4 | 3 | | |
5 | 4 | | |
| |||
17 | 16 | | |
18 | 17 | | |
19 | 18 | | |
20 | | - | |
21 | | - | |
| 19 | + | |
22 | 20 | | |
23 | | - | |
24 | | - | |
25 | | - | |
26 | | - | |
| 21 | + | |
27 | 22 | | |
| 23 | + | |
28 | 24 | | |
29 | 25 | | |
30 | 26 | | |
31 | | - | |
32 | | - | |
| 27 | + | |
0 commit comments