-
-
Notifications
You must be signed in to change notification settings - Fork 640
fix(iOS, Stack v4): prevent scroll to top when header subview is tapped on iPadOS 26+ #3731
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
kligarski
wants to merge
9
commits into
main
Choose a base branch
from
@kligarski/prevent-scroll-to-top-in-header-subview
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
fe44133
add and apply RNSScrollToTopGuardRecognizer
kligarski cf744c6
limit workaround to left, right and center subviews
kligarski 7d36d01
add issue test
kligarski ca06854
add feature flag
kligarski f02640e
build on Android
kligarski 6232ea6
Merge branch 'main' into @kligarski/prevent-scroll-to-top-in-header-s…
kligarski 37802e2
rename to RNSScrollToTopGuard*Gesture*Recognizer
kligarski 29fd3f1
don't rely on gesture recognizer count; check class instead
kligarski e62d7be
import UIKit directly, use nil action instead of _noop method
kligarski File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| import React from 'react'; | ||
| import { NavigationContainer } from '@react-navigation/native'; | ||
| import { createNativeStackNavigator } from '@react-navigation/native-stack'; | ||
| import { Button, ScrollView, Text, View } from 'react-native'; | ||
| import LongText from '../../shared/LongText'; | ||
|
|
||
| type RouteParamList = { | ||
| Screen1: undefined; | ||
| }; | ||
|
|
||
| const Stack = createNativeStackNavigator<RouteParamList>(); | ||
|
|
||
| function Screen1() { | ||
| return ( | ||
| <ScrollView> | ||
| <View | ||
| style={{ | ||
| marginVertical: 20, | ||
| marginHorizontal: 200, | ||
| justifyContent: 'center', | ||
| }}> | ||
| <Text style={{ fontWeight: 'bold' }}> | ||
| Use iPadOS 26+. Scroll down and tap the subviews. The scroll view | ||
| SHOULD NOT scroll to top unless header is tapped outside of any header | ||
| subview. | ||
| </Text> | ||
| </View> | ||
| <LongText size="xl" /> | ||
| <LongText size="xl" /> | ||
| <LongText size="xl" /> | ||
| </ScrollView> | ||
| ); | ||
| } | ||
|
|
||
| const headerButton = ( | ||
| <Button title="Click me" onPress={() => console.log('Button clicked!')} /> | ||
| ); | ||
| const headerButtonFn = () => headerButton; | ||
|
|
||
| export default function App() { | ||
| return ( | ||
| <NavigationContainer> | ||
| <Stack.Navigator> | ||
| <Stack.Screen | ||
| name="Screen1" | ||
| component={Screen1} | ||
| options={{ | ||
| headerLeft: headerButtonFn, | ||
| headerTitle: headerButtonFn, | ||
| unstable_headerRightItems: () => [ | ||
| { | ||
| type: 'custom', | ||
| element: headerButton, | ||
| hidesSharedBackground: true, | ||
| }, | ||
| ], | ||
| }} | ||
| /> | ||
| </Stack.Navigator> | ||
| </NavigationContainer> | ||
| ); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| #pragma once | ||
|
|
||
| #import <UIKit/UIKit.h> | ||
|
|
||
| NS_ASSUME_NONNULL_BEGIN | ||
|
|
||
| /** | ||
| * This gesture recognizer is necessary to handle header subviews with custom view | ||
| * on iPadOS 26+. Otherwise, clicking a custom view will result in scroll to top action even if | ||
| * there is a pressable inside the custom view. | ||
| */ | ||
| @interface RNSScrollToTopGuardGestureRecognizer : UITapGestureRecognizer <UIGestureRecognizerDelegate> | ||
|
kligarski marked this conversation as resolved.
|
||
|
|
||
| /** | ||
| * This method creates and adds the scroll to top guard gesture recognizer on iPadOS 26+. | ||
| * Otherwise, it is a no-op. | ||
| */ | ||
| + (void)applyToViewIfNecessary:(UIView *)view; | ||
|
|
||
| @end | ||
|
|
||
| NS_ASSUME_NONNULL_END | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| #import "RNSScrollToTopGuardGestureRecognizer.h" | ||
| #import "RNSDefines.h" | ||
|
|
||
| @implementation RNSScrollToTopGuardGestureRecognizer | ||
|
|
||
| #if RNS_IPHONE_OS_VERSION_AVAILABLE(26_0) | ||
|
|
||
| - (instancetype)init | ||
| { | ||
| if (self = [super initWithTarget:self action:nil]) { | ||
| self.cancelsTouchesInView = NO; | ||
| self.delegate = self; | ||
| } | ||
| return self; | ||
| } | ||
|
|
||
| #pragma mark - UIGestureRecognizerDelegate | ||
|
|
||
| - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer | ||
| shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer | ||
| { | ||
| return YES; | ||
| } | ||
|
|
||
| - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer | ||
| shouldBeRequiredToFailByGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer | ||
| { | ||
| // We want to target tap gesture recognizers only (one responsible for scroll to top must be one). | ||
| if (![otherGestureRecognizer isKindOfClass:[UITapGestureRecognizer class]]) { | ||
| return NO; | ||
| } | ||
|
|
||
| // We don't know the exact recognizer responsible for scroll to top so we block all | ||
| // gesture recognizers above us. | ||
| return ![otherGestureRecognizer.view isDescendantOfView:gestureRecognizer.view]; | ||
| } | ||
|
|
||
| #endif // RNS_IPHONE_OS_VERSION_AVAILABLE(26_0) | ||
|
|
||
| + (void)applyToViewIfNecessary:(nonnull UIView *)view | ||
| { | ||
| #if RNS_IPHONE_OS_VERSION_AVAILABLE(26_0) | ||
| if (@available(iOS 26.0, *)) { | ||
| if (UIDevice.currentDevice.userInterfaceIdiom == UIUserInterfaceIdiomPad) { | ||
| [view addGestureRecognizer:[RNSScrollToTopGuardGestureRecognizer new]]; | ||
| } | ||
| } | ||
| #endif // RNS_IPHONE_OS_VERSION_AVAILABLE(26_0) | ||
| } | ||
|
|
||
| @end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if only applicable to 26+, could we exclude this code from lower versions? then
applyToViewIfNecessarycould be iPadOS26+ either and we fully hide this fix from other OS versions