Restore content offset after background snapshot on iOS #55026
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary:
In iOS 26, backgrounding an app triggers an app snapshot [1], which will be taken in all supported device orientations. When in landscape mode, the OS will make a layout pass in portrait followed by another in landscape (and vice versa), taking a snapshot of each.
If scrolled to the bottom of a ScrollView in landscape mode, the content offset will be incorrect on resume, as UIScrollView updates the offset during the portrait layout pass to prevent overscroll. When the next layout pass is made, the offset is not reset to the original value.
Store the content offset on update when foregrounded. When backgrounded, override the content offset with the stored content offset, clamped to the content size. When the second snapshot is taken (in the original orientation), the original content offset is restored.
Fixes #54979.
[1] https://developer.apple.com/documentation/uikit/preparing-your-ui-to-run-in-the-background#Prepare-your-UI-for-the-app-snapshot
Changelog:
[IOS] [FIXED] - Scroll position not maintained after resume on iOS
Test Plan:
Reproducer: https://snack.expo.dev/@mhoran/restless-green-graham-crackers
Per #54979:
Without this patch, the ScrollView position will change, due to the background snapshot taken in portrait mode.
Screen recording: https://drive.google.com/file/d/1WrEXNSF2dcYswtKrlM6YJglGiOzoGq8u/view?usp=drive_link.