Skip to content

chore(iOS): refactor back button logic #2861

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

Closed
wants to merge 10 commits into from
30 changes: 12 additions & 18 deletions ios/RNSScreenStackHeaderConfig.mm
Original file line number Diff line number Diff line change
Expand Up @@ -533,20 +533,21 @@ + (void)updateViewController:(UIViewController *)vc
#if !TARGET_OS_TV
const auto isBackTitleBlank = [NSString rnscreens_isBlankOrNull:config.backTitle] == YES;
NSString *resolvedBackTitle = isBackTitleBlank ? prevItem.title : config.backTitle;
RNSUIBarButtonItem *backBarButtonItem = [[RNSUIBarButtonItem alloc] initWithTitle:resolvedBackTitle
style:UIBarButtonItemStylePlain
target:nil
action:nil];
[backBarButtonItem setMenuHidden:config.disableBackButtonMenu];

auto shouldUseCustomBackBarButtonItem = config.disableBackButtonMenu;
prevItem.backButtonTitle = resolvedBackTitle;

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

if (config.isBackTitleVisible) {
auto shouldUseCustomBackBarButtonItem = config.disableBackButtonMenu;
RNSUIBarButtonItem *backBarButtonItem = [[RNSUIBarButtonItem alloc] initWithTitle:resolvedBackTitle
style:UIBarButtonItemStylePlain
target:nil
action:nil];
[backBarButtonItem setMenuHidden:config.disableBackButtonMenu];

if ((config.backTitleFontFamily &&
// While being used by react-navigation, the `backTitleFontFamily` will
// be set to "System" by default - which is the system default font.
Expand All @@ -570,19 +571,12 @@ + (void)updateViewController:(UIViewController *)vc
}
[self setTitleAttibutes:attrs forButton:backBarButtonItem];
}

if (shouldUseCustomBackBarButtonItem) {
prevItem.backBarButtonItem = backBarButtonItem;
}
} else {
// back button title should be not visible next to back button,
// but it should still appear in back menu
prevItem.backButtonDisplayMode = UINavigationItemBackButtonDisplayModeMinimal;
shouldUseCustomBackBarButtonItem = NO;
}

// Prevent unnecessary assignment of backBarButtonItem if it is not customized,
// as assigning one will override the native behavior of automatically shortening
// the title to "Back" or hide the back title if there's not enough space.
// See: https://github.com/software-mansion/react-native-screens/issues/1589
if (shouldUseCustomBackBarButtonItem) {
prevItem.backBarButtonItem = backBarButtonItem;
}

if (@available(iOS 11.0, *)) {
Expand Down
Loading