Skip to content

Commit 0208e88

Browse files
authored
fix(iOS): fix header back button display mode when back title text is custom (#2860)
## Description Fixes #2809 ⚠️ This change requires [react-navigation](react-navigation/react-navigation#12543) changed to merged first and submodule bumped for test to pass. This PR solves an issue, when using custom backTitle caused usage of the backButtonItem, which in turn disabled backButtonDisplayMode. This cause inconsistent behavior with UIKit - the back button label wasn't reduced to Back or chevron as it is does for [backButtonDisplayMode](https://developer.apple.com/documentation/uikit/uinavigationitem/backbuttondisplaymode-swift.property?language=objc). Coresponding [react-navigation changes](react-navigation/react-navigation#12543). ## Changes - Remove check if the backTitle is filled from condition that enforces backButtonItem usage and set backTitle by default. - Update docs - Update tests ## Test code and steps to reproduce see `FabricExample/e2e/issuesTests/Test2809.e2e.ts` and `apps/src/tests/Test2809/index.tsx` ## Checklist - [x] Included code example that can be used to test this change - [x] Updated TS types - [x] Updated documentation: <!-- For adding new props to native-stack --> - [x] https://github.com/software-mansion/react-native-screens/blob/main/guides/GUIDE_FOR_LIBRARY_AUTHORS.md - [x] https://github.com/software-mansion/react-native-screens/blob/main/native-stack/README.md - [x] https://github.com/software-mansion/react-native-screens/blob/main/src/types.tsx - [x] https://github.com/software-mansion/react-native-screens/blob/main/src/native-stack/types.tsx - [ ] Ensured that CI passes
1 parent 34591a8 commit 0208e88

7 files changed

Lines changed: 12 additions & 24 deletions

File tree

FabricExample/e2e/issuesTests/Test2809.e2e.ts

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,9 @@ describeIfiOS('Test2809', () => {
8989
await expectBackButtonMenuWithDifferentLabels('Back', 'First'); // Check if backButtonMenu works
9090
});
9191

92-
// TODO: We should be able to fix that
93-
// Custom text overrides backButtonDisplayMode
94-
it('custom text is NOT truncated by backButtonDisplayMode and is used in back button menu', async () => {
95-
await expectInitialPageToExist('EnabledGenericCustomText', by.text('Custom'));
96-
await expectBackButtonMenuWithTheSameLabel('Custom'); // Check if backButtonMenu works
92+
it('custom text is truncated by backButtonDisplayMode and is used in back button menu', async () => {
93+
await expectInitialPageToExist('EnabledGenericCustomText', by.text('Back'));
94+
await expectBackButtonMenuWithDifferentLabels('Back', 'Custom'); // Check if backButtonMenu works
9795
});
9896

9997
// Custom styles override backButtonDisplayMode
@@ -195,17 +193,13 @@ describeIfiOS('Test2809', () => {
195193
await element(by.text('Pop to top')).tap(); // Go back
196194
});
197195

198-
// TODO: We should be able to fix that
199-
// Custom text overrides backButtonDisplayMode because of using backButtonItem
200-
it.failing('Custom long back label should be truncated to generic by backButtonDisplayMode', async () => {
196+
it('Custom long back label should be truncated to generic by backButtonDisplayMode', async () => {
201197
await expectInitialPageToExist('CustomLongCustomText', by.text('Back'));
202198
await expectBackButtonMenuWithDifferentLabels('Back', 'LongLongLongLongLong'); // Check if backButtonMenu works
203199
await element(by.text('Pop to top')).tap(); // Go back
204200
});
205201

206-
// TODO: We should be able to fix that
207-
// Custom text overrides backButtonDisplayMode because of using backButtonItem
208-
it.failing('Custom back label should be truncated to minimal by backButtonDisplayMode when title is long', async () => {
202+
it('Custom back label should be truncated to minimal by backButtonDisplayMode when title is long', async () => {
209203
await expectInitialPageToExist('CustomCustomTextWithLongTitle', by.id('chevron.backward'), by.text('CustomBack'));
210204
await expectBackButtonMenuIconAndLabel('chevron.backward', 'CustomBack'); // Check if backButtonMenu works
211205
await element(by.text('Pop to top')).tap(); // Go back

apps/src/tests/Test2809/index.tsx

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -153,11 +153,6 @@ function Home({
153153
title="CustomCustomTextWithLongTitle"
154154
onPress={() => navigation.navigate('CustomCustomTextWithLongTitle')}
155155
/>
156-
157-
<Button
158-
title="Go back"
159-
onPress={() => navigation.goBack()}
160-
/>
161156
</View>
162157
);
163158
}

guides/GUIDE_FOR_LIBRARY_AUTHORS.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -532,7 +532,7 @@ Boolean indicating whether to show the menu on longPress of iOS >= 14 back butto
532532

533533
### `backButtonDisplayMode` (iOS only)
534534

535-
Enum value indicating display mode of back button. It is used only when none of: `backTitleFontFamily`, `backTitleFontSize`, `disableBackButtonMenu`, `backTitle` and `backTitleVisible=false` is set. The `backTitleVisible` forces `backButtonDisplayMode: minimal` and omits other values. Read more [#2800](https://github.com/software-mansion/react-native-screens/pull/2800). The other props, under the hood, customize `backButtonItem` which overrides `backButtonDisplayMode`. Read more [#2123](https://github.com/software-mansion/react-native-screens/pull/2123).
535+
Enum value indicating display mode of back button. It is used only when none of: `backTitleFontFamily`, `backTitleFontSize`, `disableBackButtonMenu` and `backTitleVisible=false` is set. The `backTitleVisible` forces `backButtonDisplayMode: minimal` and omits other values. Read more [#2800](https://github.com/software-mansion/react-native-screens/pull/2800). The other props, under the hood, customize `backButtonItem` which overrides `backButtonDisplayMode`. Read more [#2123](https://github.com/software-mansion/react-native-screens/pull/2123).
536536

537537
Possible options:
538538

ios/RNSScreenStackHeaderConfig.mm

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -629,11 +629,12 @@ + (void)updateViewController:(UIViewController *)vc
629629
action:nil];
630630
[backBarButtonItem setMenuHidden:config.disableBackButtonMenu];
631631

632-
auto shouldUseCustomBackBarButtonItem = !isBackTitleBlank || config.disableBackButtonMenu;
632+
auto shouldUseCustomBackBarButtonItem = config.disableBackButtonMenu;
633633

634634
// This has any effect only in case the `backBarButtonItem` is not set.
635635
// We apply it before we configure the back item, because it might get overriden.
636636
prevItem.backButtonDisplayMode = config.backButtonDisplayMode;
637+
prevItem.backButtonTitle = resolvedBackTitle;
637638

638639
if (config.isBackTitleVisible) {
639640
if ((config.backTitleFontFamily &&
@@ -661,9 +662,7 @@ + (void)updateViewController:(UIViewController *)vc
661662
}
662663
} else {
663664
// back button title should be not visible next to back button,
664-
// but it should still appear in back menu (if one is enabled)
665-
666-
prevItem.backButtonTitle = resolvedBackTitle;
665+
// but it should still appear in back menu
667666
prevItem.backButtonDisplayMode = UINavigationItemBackButtonDisplayModeMinimal;
668667
shouldUseCustomBackBarButtonItem = NO;
669668
}

native-stack/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ Boolean indicating whether to show the menu on longPress of iOS >= 14 back butto
8282

8383
#### `backButtonDisplayMode` (iOS only)
8484

85-
Enum value indicating display mode of back button. It is used only when none of: `backTitleFontFamily`, `backTitleFontSize`, `disableBackButtonMenu`, `backTitle` and `backTitleVisible=false` is set. The `backTitleVisible` forces `backButtonDisplayMode: minimal` and omits other values. Read more [#2800](https://github.com/software-mansion/react-native-screens/pull/2800). The other props, under the hood, customize `backButtonItem` which overrides `backButtonDisplayMode`. Read more [#2123](https://github.com/software-mansion/react-native-screens/pull/2123).
85+
Enum value indicating display mode of back button. It is used only when none of: `backTitleFontFamily`, `backTitleFontSize`, `disableBackButtonMenu` and `backTitleVisible=false` is set. The `backTitleVisible` forces `backButtonDisplayMode: minimal` and omits other values. Read more [#2800](https://github.com/software-mansion/react-native-screens/pull/2800). The other props, under the hood, customize `backButtonItem` which overrides `backButtonDisplayMode`. Read more [#2123](https://github.com/software-mansion/react-native-screens/pull/2123).
8686

8787
Possible options:
8888

src/native-stack/types.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ export type NativeStackNavigationOptions = {
140140
*/
141141
disableBackButtonMenu?: boolean;
142142
/**
143-
* How the back button behaves. It is used only when none of: `backTitleFontFamily`, `backTitleFontSize`, `disableBackButtonMenu`, `backTitle` and `backTitleVisible=false` is set.
143+
* How the back button behaves. It is used only when none of: `backTitleFontFamily`, `backTitleFontSize`, `disableBackButtonMenu` and `backTitleVisible=false` is set.
144144
* The following values are currently supported (they correspond to [UINavigationItemBackButtonDisplayMode](https://developer.apple.com/documentation/uikit/uinavigationitembackbuttondisplaymode?language=objc)):
145145
*
146146
* - `default` – show given back button previous controller title, system generic or just icon based on available space

src/types.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -576,7 +576,7 @@ export interface ScreenStackHeaderConfigProps extends ViewProps {
576576
*/
577577
disableBackButtonMenu?: boolean;
578578
/**
579-
* How the back button behaves. It is used only when none of: `backTitleFontFamily`, `backTitleFontSize`, `disableBackButtonMenu`, `backTitle` and `backTitleVisible=false` is set.
579+
* How the back button behaves. It is used only when none of: `backTitleFontFamily`, `backTitleFontSize`, `disableBackButtonMenu` and `backTitleVisible=false` is set.
580580
* The following values are currently supported (they correspond to [UINavigationItemBackButtonDisplayMode](https://developer.apple.com/documentation/uikit/uinavigationitembackbuttondisplaymode?language=objc)):
581581
*
582582
* - `default` – show given back button previous controller title, system generic or just icon based on available space

0 commit comments

Comments
 (0)