Skip to content
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

fix(iOS): header left and right layout on fabric #2248

Merged
merged 6 commits into from
Jul 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions apps/src/tests/Test2231.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { createNativeStackNavigator } from '@react-navigation/native-stack';
import React, { useLayoutEffect, useState } from 'react';
import { View } from 'react-native';

import { SettingsSwitch, Square } from '../shared';
import { NavigationContainer } from '@react-navigation/native';

const SettingsScreen = ({ navigation }: any) => {
const [hasLeftItem, setHasLeftItem] = useState(false);

const square1 = (props: { tintColor?: string }) => (
<View style={{ gap: 8, flexDirection: 'row' }}>
{hasLeftItem && <Square {...props} color="green" size={20} />}
<Square {...props} color="green" size={20} />
</View>
);

const square2 = (props: { tintColor?: string }) => (
<Square {...props} color="red" size={20} />
);

useLayoutEffect(() => {
navigation.setOptions({
headerRight: square1,
headerTitle: undefined,
headerLeft: hasLeftItem ? square2 : undefined,
headerBackTitleVisible: false,
});
}, [navigation, hasLeftItem]);

return (
<SettingsSwitch
label="Left item"
value={hasLeftItem}
onValueChange={setHasLeftItem}
/>
);
};

const Stack = createNativeStackNavigator();

const App = () => (
<NavigationContainer>
<Stack.Navigator>
<Stack.Screen
name="Settings"
component={SettingsScreen}
options={{
headerTintColor: 'hotpink',
}}
/>
</Stack.Navigator>
</NavigationContainer>
);

export default App;
1 change: 1 addition & 0 deletions apps/src/tests/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,5 +104,6 @@ export { default as Test2184 } from './Test2184';
export { default as Test2223 } from './Test2223';
export { default as Test2227 } from './Test2227';
export { default as Test2229 } from './Test2229';
export { default as Test2231 } from './Test2231';
export { default as TestScreenAnimation } from './TestScreenAnimation';
export { default as TestHeader } from './TestHeader';
3 changes: 3 additions & 0 deletions ios/RNSScreenStackHeaderSubview.mm
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ - (void)updateLayoutMetrics:(const react::LayoutMetrics &)layoutMetrics
self);
} else {
self.bounds = CGRect{CGPointZero, frame.size};
// We're forcing the parent view to layout this subview with correct frame size,
// see: https://github.com/software-mansion/react-native-screens/pull/2248
[self.superview layoutIfNeeded];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, so we now trigger layout in the native superview (this is one of system views, beyond control of react) every time we receive a new frame from react.

The solution itself is ok & if it helps I'm for it, but do we have understanding why does it not work without this change? Especially that we do not have such code on Paper & I believe it works fine there?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, it does work fine on paper. It looks like the bounds of the subview are set properly but then the frame is given the old values unless we trigger a re-layout of the superview.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you check what happens if you disable view recycling on header subviews in this PR? Im speaking with context of this issue on which we debated earlier today: #2232

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might be the same issue of the views being swapped due to recycling pool working in stack manner.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Turning off recycling does not fix this issue

}
}

Expand Down
Loading