Skip to content

Commit 0e418c1

Browse files
maciekstosiokkafar
andauthored
refactor(iOS): limit scope of shouldUseCustomBackBarButtonItem flag (#2867)
## Description / Changes This is continuation of #2860 that proposes small refactor around back button logic. This area is very confusing and in my opinion this is due to flag `shouldUseCustomBackBarButtonItem` that breaks the flow of other if's and causes other props to not work. Not we contained this flag in `if (visible)` which currently is the only case when backButtonItem might be used after the changes from #2800. ## Test code and steps to reproduce see `FabricExample/e2e/issuesTests/Test2809.e2e.ts` and `apps/src/tests/Test2809/index.tsx` ## Checklist - [ ] Ensured that CI passes --------- Co-authored-by: Kacper Kafara <[email protected]>
1 parent e578a8e commit 0e418c1

File tree

1 file changed

+16
-18
lines changed

1 file changed

+16
-18
lines changed

ios/RNSScreenStackHeaderConfig.mm

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -678,20 +678,19 @@ - (void)configureBackItem:(nullable UINavigationItem *)prevItem API_UNAVAILABLE(
678678

679679
const auto isBackTitleBlank = [NSString rnscreens_isBlankOrNull:config.backTitle] == YES;
680680
NSString *resolvedBackTitle = isBackTitleBlank ? prevItem.title : config.backTitle;
681-
RNSUIBarButtonItem *backBarButtonItem = [[RNSUIBarButtonItem alloc] initWithTitle:resolvedBackTitle
682-
style:UIBarButtonItemStylePlain
683-
target:nil
684-
action:nil];
685-
[backBarButtonItem setMenuHidden:config.disableBackButtonMenu];
686-
687-
auto shouldUseCustomBackBarButtonItem = config.disableBackButtonMenu;
688-
681+
prevItem.backButtonTitle = resolvedBackTitle;
689682
// This has any effect only in case the `backBarButtonItem` is not set.
690683
// We apply it before we configure the back item, because it might get overriden.
691684
prevItem.backButtonDisplayMode = config.backButtonDisplayMode;
692-
prevItem.backButtonTitle = resolvedBackTitle;
693685

694686
if (config.isBackTitleVisible) {
687+
RNSUIBarButtonItem *backBarButtonItem = [[RNSUIBarButtonItem alloc] initWithTitle:resolvedBackTitle
688+
style:UIBarButtonItemStylePlain
689+
target:nil
690+
action:nil];
691+
auto shouldUseCustomBackBarButtonItem = config.disableBackButtonMenu;
692+
[backBarButtonItem setMenuHidden:config.disableBackButtonMenu];
693+
695694
if ((config.backTitleFontFamily &&
696695
// While being used by react-navigation, the `backTitleFontFamily` will
697696
// be set to "System" by default - which is the system default font.
@@ -715,19 +714,18 @@ - (void)configureBackItem:(nullable UINavigationItem *)prevItem API_UNAVAILABLE(
715714
}
716715
[RNSScreenStackHeaderConfig setTitleAttibutes:attrs forButton:backBarButtonItem];
717716
}
717+
718+
// Prevent unnecessary assignment of backBarButtonItem if it is not customized,
719+
// as assigning one will override the native behavior of automatically shortening
720+
// the title to "Back" or hide the back title if there's not enough space.
721+
// See: https://github.com/software-mansion/react-native-screens/issues/1589
722+
if (shouldUseCustomBackBarButtonItem) {
723+
prevItem.backBarButtonItem = backBarButtonItem;
724+
}
718725
} else {
719726
// back button title should be not visible next to back button,
720727
// but it should still appear in back menu
721728
prevItem.backButtonDisplayMode = UINavigationItemBackButtonDisplayModeMinimal;
722-
shouldUseCustomBackBarButtonItem = NO;
723-
}
724-
725-
// Prevent unnecessary assignment of backBarButtonItem if it is not customized,
726-
// as assigning one will override the native behavior of automatically shortening
727-
// the title to "Back" or hide the back title if there's not enough space.
728-
// See: https://github.com/software-mansion/react-native-screens/issues/1589
729-
if (shouldUseCustomBackBarButtonItem) {
730-
prevItem.backBarButtonItem = backBarButtonItem;
731729
}
732730
#endif
733731
}

0 commit comments

Comments
 (0)