Files
react-native/React/Views/ScrollView/RCTScrollContentView.m
T
Mike a4526bcc3f fix: incorrect ScrollView offset on update (#30647)
Summary:
This pull request is to fix https://github.com/facebook/react-native/issues/30258.

After an investigation, I found out the scroll offset of the list seems to be calculated incorrectly due to a workaround in the source code.

Instead of fixing the `calculateOffsetForContentSize`, I chose to remove it, the reason why I do so is because this workaround is for fixing the offset when `contentSize` is set manually, but according to the source code, there is no interface for a react-native user to set the `contentSize` of ScrollView, it is just set to `GCSizeZero` and will never be changed ([ref](https://github.com/facebook/react-native/pull/30647/files#diff-cf6f991f585ebf4cfdd555fe474e1f9ce40c2e4f823fc3f42b549414639c8c30L304)).

Also I changed the function name from `updateContentOffsetIfNeeded` to  `updateContentSizeIfNeeded` according what the function is actually doing.

## Changelog

<!-- Help reviewers and the release process by writing your own changelog entry. For an example, see:
https://github.com/facebook/react-native/wiki/Changelog
-->

[iOS] [Fixed] - Incorrect ScrollView offset on update

Pull Request resolved: https://github.com/facebook/react-native/pull/30647

Test Plan:
1. Create a fresh new project named `testapp` using npx create-react-native-app
2. Apply example code to `testapp` from https://snack.expo.io/lokomass/flatlist
3. Run `testapp` on iOS emulator and reproduce the bug
4. Make changes to files in `testapp/node_modules/react-native/`
5. Rebuild `testapp` and run on iOS emulator again, the bug is no more exist
6. Apply changes from step 4 to react-native, make a pull request.

#### Screenshots

Before: The scroll offset is incorrect after children of FlatList has changed

https://user-images.githubusercontent.com/48589760/103155130-b54a0580-47d7-11eb-97af-bdfd3e728714.mov

After: No more incorrect scroll offset if children of FlatList has changed

https://user-images.githubusercontent.com/48589760/103155091-6ef4a680-47d7-11eb-89fa-6f708bfef1c9.mov

Reviewed By: sammy-SC

Differential Revision: D25732958

Pulled By: shergin

fbshipit-source-id: dac6eff15ac3bbfec502452ac14b3d49fee76c29
2021-01-05 11:06:30 -08:00

33 lines
702 B
Objective-C

/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#import "RCTScrollContentView.h"
#import <React/RCTAssert.h>
#import <React/UIView+React.h>
#import "RCTScrollView.h"
@implementation RCTScrollContentView
- (void)reactSetFrame:(CGRect)frame
{
[super reactSetFrame:frame];
RCTScrollView *scrollView = (RCTScrollView *)self.superview.superview;
if (!scrollView) {
return;
}
RCTAssert([scrollView isKindOfClass:[RCTScrollView class]], @"Unexpected view hierarchy of RCTScrollView component.");
[scrollView updateContentSizeIfNeeded];
}
@end