Skip to content

Commit e3e34f3

Browse files
committed
Move back item configuration to dedicated method
1 parent 49aee9d commit e3e34f3

1 file changed

Lines changed: 64 additions & 61 deletions

File tree

ios/RNSScreenStackHeaderConfig.mm

Lines changed: 64 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -534,69 +534,13 @@ + (void)updateViewController:(UIViewController *)vc
534534
}
535535

536536
#if !TARGET_OS_TV
537-
const auto isBackTitleBlank = [NSString rnscreens_isBlankOrNull:config.backTitle] == YES;
538-
NSString *resolvedBackTitle = isBackTitleBlank ? prevItem.title : config.backTitle;
539-
RNSUIBarButtonItem *backBarButtonItem = [[RNSUIBarButtonItem alloc] initWithTitle:resolvedBackTitle
540-
style:UIBarButtonItemStylePlain
541-
target:nil
542-
action:nil];
543-
[backBarButtonItem setMenuHidden:config.disableBackButtonMenu];
544-
545-
auto shouldUseCustomBackBarButtonItem = config.disableBackButtonMenu;
546-
547-
// This has any effect only in case the `backBarButtonItem` is not set.
548-
// We apply it before we configure the back item, because it might get overriden.
549-
prevItem.backButtonDisplayMode = config.backButtonDisplayMode;
550-
prevItem.backButtonTitle = resolvedBackTitle;
551-
552-
if (config.isBackTitleVisible) {
553-
if ((config.backTitleFontFamily &&
554-
// While being used by react-navigation, the `backTitleFontFamily` will
555-
// be set to "System" by default - which is the system default font.
556-
// To avoid always considering the font as customized, we need to have an additional check.
557-
// See: https://github.com/software-mansion/react-native-screens/pull/2105#discussion_r1565222738
558-
![config.backTitleFontFamily isEqual:@"System"]) ||
559-
config.backTitleFontSize) {
560-
shouldUseCustomBackBarButtonItem = YES;
561-
NSMutableDictionary *attrs = [NSMutableDictionary new];
562-
NSNumber *size = config.backTitleFontSize ?: @17;
563-
if (config.backTitleFontFamily) {
564-
attrs[NSFontAttributeName] = [RCTFont updateFont:nil
565-
withFamily:config.backTitleFontFamily
566-
size:size
567-
weight:nil
568-
style:nil
569-
variant:nil
570-
scaleMultiplier:1.0];
571-
} else {
572-
attrs[NSFontAttributeName] = [UIFont boldSystemFontOfSize:[size floatValue]];
573-
}
574-
[self setTitleAttibutes:attrs forButton:backBarButtonItem];
575-
}
576-
} else {
577-
// back button title should be not visible next to back button,
578-
// but it should still appear in back menu
579-
prevItem.backButtonDisplayMode = UINavigationItemBackButtonDisplayModeMinimal;
580-
shouldUseCustomBackBarButtonItem = NO;
581-
}
582-
583-
// Prevent unnecessary assignment of backBarButtonItem if it is not customized,
584-
// as assigning one will override the native behavior of automatically shortening
585-
// the title to "Back" or hide the back title if there's not enough space.
586-
// See: https://github.com/software-mansion/react-native-screens/issues/1589
587-
if (shouldUseCustomBackBarButtonItem) {
588-
prevItem.backBarButtonItem = backBarButtonItem;
589-
}
590-
591537
[config configureBackItem:prevItem];
592538

593-
if (@available(iOS 11.0, *)) {
594-
if (config.largeTitle) {
595-
navctr.navigationBar.prefersLargeTitles = YES;
596-
}
597-
navitem.largeTitleDisplayMode =
598-
config.largeTitle ? UINavigationItemLargeTitleDisplayModeAlways : UINavigationItemLargeTitleDisplayModeNever;
539+
if (config.largeTitle) {
540+
navctr.navigationBar.prefersLargeTitles = YES;
599541
}
542+
navitem.largeTitleDisplayMode =
543+
config.largeTitle ? UINavigationItemLargeTitleDisplayModeAlways : UINavigationItemLargeTitleDisplayModeNever;
600544
#endif
601545

602546
UINavigationBarAppearance *appearance = [self buildAppearance:vc withConfig:config];
@@ -723,8 +667,67 @@ + (void)updateViewController:(UIViewController *)vc
723667
}
724668
}
725669

726-
- (void)configureBackItem:(UINavigationItem *)prevItem
670+
- (void)configureBackItem:(nullable UINavigationItem *)prevItem
727671
{
672+
if (prevItem == nil) {
673+
return;
674+
}
675+
676+
const auto *config = self;
677+
678+
const auto isBackTitleBlank = [NSString rnscreens_isBlankOrNull:config.backTitle] == YES;
679+
NSString *resolvedBackTitle = isBackTitleBlank ? prevItem.title : config.backTitle;
680+
RNSUIBarButtonItem *backBarButtonItem = [[RNSUIBarButtonItem alloc] initWithTitle:resolvedBackTitle
681+
style:UIBarButtonItemStylePlain
682+
target:nil
683+
action:nil];
684+
[backBarButtonItem setMenuHidden:config.disableBackButtonMenu];
685+
686+
auto shouldUseCustomBackBarButtonItem = config.disableBackButtonMenu;
687+
688+
// This has any effect only in case the `backBarButtonItem` is not set.
689+
// We apply it before we configure the back item, because it might get overriden.
690+
prevItem.backButtonDisplayMode = config.backButtonDisplayMode;
691+
prevItem.backButtonTitle = resolvedBackTitle;
692+
693+
if (config.isBackTitleVisible) {
694+
if ((config.backTitleFontFamily &&
695+
// While being used by react-navigation, the `backTitleFontFamily` will
696+
// be set to "System" by default - which is the system default font.
697+
// To avoid always considering the font as customized, we need to have an additional check.
698+
// See: https://github.com/software-mansion/react-native-screens/pull/2105#discussion_r1565222738
699+
![config.backTitleFontFamily isEqual:@"System"]) ||
700+
config.backTitleFontSize) {
701+
shouldUseCustomBackBarButtonItem = YES;
702+
NSMutableDictionary *attrs = [NSMutableDictionary new];
703+
NSNumber *size = config.backTitleFontSize ?: @17;
704+
if (config.backTitleFontFamily) {
705+
attrs[NSFontAttributeName] = [RCTFont updateFont:nil
706+
withFamily:config.backTitleFontFamily
707+
size:size
708+
weight:nil
709+
style:nil
710+
variant:nil
711+
scaleMultiplier:1.0];
712+
} else {
713+
attrs[NSFontAttributeName] = [UIFont boldSystemFontOfSize:[size floatValue]];
714+
}
715+
[RNSScreenStackHeaderConfig setTitleAttibutes:attrs forButton:backBarButtonItem];
716+
}
717+
} else {
718+
// back button title should be not visible next to back button,
719+
// but it should still appear in back menu
720+
prevItem.backButtonDisplayMode = UINavigationItemBackButtonDisplayModeMinimal;
721+
shouldUseCustomBackBarButtonItem = NO;
722+
}
723+
724+
// Prevent unnecessary assignment of backBarButtonItem if it is not customized,
725+
// as assigning one will override the native behavior of automatically shortening
726+
// the title to "Back" or hide the back title if there's not enough space.
727+
// See: https://github.com/software-mansion/react-native-screens/issues/1589
728+
if (shouldUseCustomBackBarButtonItem) {
729+
prevItem.backBarButtonItem = backBarButtonItem;
730+
}
728731
}
729732

730733
- (void)applySemanticContentAttributeIfNeededToNavCtrl:(UINavigationController *)navCtrl

0 commit comments

Comments
 (0)