react-native-navigation的迁移库

TopBarPresenter.m 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. #import "TopBarPresenter.h"
  2. #import "UIImage+tint.h"
  3. #import "RNNFontAttributesCreator.h"
  4. #import "UIColor+RNNUtils.h"
  5. @implementation TopBarPresenter
  6. - (instancetype)initWithNavigationController:(UINavigationController *)boundNavigationController {
  7. self = [super init];
  8. self.boundViewController = boundNavigationController;
  9. return self;
  10. }
  11. - (void)applyOptions:(RNNTopBarOptions *)options {
  12. [self setTranslucent:[options.background.translucent getWithDefaultValue:NO]];
  13. [self setBackgroundColor:[options.background.color getWithDefaultValue:nil]];
  14. [self setTitleAttributes:options.title];
  15. [self setLargeTitleAttributes:options.largeTitle];
  16. [self showBorder:![options.noBorder getWithDefaultValue:NO]];
  17. [self setBackButtonOptions:options.backButton];
  18. }
  19. - (void)applyOptionsBeforePopping:(RNNTopBarOptions *)options {
  20. [self setBackgroundColor:[options.background.color getWithDefaultValue:nil]];
  21. [self setTitleAttributes:options.title];
  22. [self setLargeTitleAttributes:options.largeTitle];
  23. }
  24. - (void)mergeOptions:(RNNTopBarOptions *)options withDefault:(RNNTopBarOptions *)withDefault {
  25. if (options.background.color.hasValue) {
  26. [self setBackgroundColor:options.background.color.get];
  27. }
  28. if (options.noBorder.hasValue) {
  29. [self showBorder:![options.noBorder get]];
  30. }
  31. if (options.background.translucent.hasValue) {
  32. [self setTranslucent:[options.background.translucent get]];
  33. }
  34. RNNLargeTitleOptions* largeTitleOptions = options.largeTitle;
  35. if (largeTitleOptions.color.hasValue || largeTitleOptions.fontSize.hasValue || largeTitleOptions.fontFamily.hasValue) {
  36. [self setLargeTitleAttributes:largeTitleOptions];
  37. }
  38. if (options.title.hasValue) {
  39. [self setTitleAttributes:withDefault.title];
  40. }
  41. if (options.backButton.hasValue) {
  42. [self setBackButtonOptions:withDefault.backButton];
  43. }
  44. }
  45. - (UINavigationController *)navigationController {
  46. return (UINavigationController *)self.boundViewController;
  47. }
  48. - (void)showBorder:(BOOL)showBorder {
  49. [self.navigationController.navigationBar setShadowImage:showBorder ? nil : [UIImage new]];
  50. }
  51. - (void)setBackIndicatorImage:(UIImage *)image withColor:(UIColor *)color {
  52. [self.navigationController.navigationBar setBackIndicatorImage:image];
  53. [self.navigationController.navigationBar setBackIndicatorTransitionMaskImage:image];
  54. }
  55. - (void)setBackgroundColor:(UIColor *)backgroundColor {
  56. _backgroundColor = backgroundColor;
  57. [self updateBackgroundAppearance];
  58. }
  59. - (void)updateBackgroundAppearance {
  60. if (self.transparent) {
  61. [self setBackgroundColorTransparent];
  62. } else if (_backgroundColor) {
  63. self.navigationController.navigationBar.translucent = NO;
  64. self.navigationController.navigationBar.barTintColor = _backgroundColor;
  65. } else if (_translucent) {
  66. self.navigationController.navigationBar.translucent = YES;
  67. } else {
  68. self.navigationController.navigationBar.translucent = NO;
  69. self.navigationController.navigationBar.barTintColor = nil;
  70. }
  71. }
  72. - (void)setBackgroundColorTransparent {
  73. self.navigationController.navigationBar.barTintColor = UIColor.clearColor;
  74. self.navigationController.navigationBar.translucent = YES;
  75. self.navigationController.navigationBar.shadowImage = [UIImage new];
  76. [self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
  77. }
  78. - (void)setTitleAttributes:(RNNTitleOptions *)titleOptions {
  79. NSString* fontFamily = [titleOptions.fontFamily getWithDefaultValue:nil];
  80. NSString* fontWeight = [titleOptions.fontWeight getWithDefaultValue:nil];
  81. NSNumber* fontSize = [titleOptions.fontSize getWithDefaultValue:nil];
  82. UIColor* fontColor = [titleOptions.color getWithDefaultValue:nil];
  83. self.navigationController.navigationBar.titleTextAttributes = [RNNFontAttributesCreator createFromDictionary:self.navigationController.navigationBar.titleTextAttributes fontFamily:fontFamily fontSize:fontSize defaultFontSize:nil fontWeight:fontWeight color:fontColor defaultColor:nil];
  84. }
  85. - (void)setLargeTitleAttributes:(RNNLargeTitleOptions *)largeTitleOptions {
  86. NSString* fontFamily = [largeTitleOptions.fontFamily getWithDefaultValue:nil];
  87. NSString* fontWeight = [largeTitleOptions.fontWeight getWithDefaultValue:nil];
  88. NSNumber* fontSize = [largeTitleOptions.fontSize getWithDefaultValue:nil];
  89. UIColor* fontColor = [largeTitleOptions.color getWithDefaultValue:nil];
  90. if (@available(iOS 11.0, *)) {
  91. self.navigationController.navigationBar.largeTitleTextAttributes = [RNNFontAttributesCreator createFromDictionary:self.navigationController.navigationBar.largeTitleTextAttributes fontFamily:fontFamily fontSize:fontSize defaultFontSize:nil fontWeight:fontWeight color:fontColor defaultColor:nil];
  92. }
  93. }
  94. - (void)setBackButtonOptions:(RNNBackButtonOptions *)backButtonOptions {
  95. UIImage* icon = [backButtonOptions.icon getWithDefaultValue:nil];
  96. UIColor* color = [backButtonOptions.color getWithDefaultValue:nil];
  97. NSString* title = [backButtonOptions.title getWithDefaultValue:nil];
  98. BOOL showTitle = [backButtonOptions.showTitle getWithDefaultValue:YES];
  99. NSString* fontFamily = [backButtonOptions.fontFamily getWithDefaultValue:nil];
  100. NSNumber* fontSize = [backButtonOptions.fontSize getWithDefaultValue:nil];
  101. NSString* testID = [backButtonOptions.testID getWithDefaultValue:nil];
  102. NSArray* stackChildren = self.navigationController.viewControllers;
  103. UIViewController *lastViewControllerInStack = stackChildren.count > 1 ? stackChildren[stackChildren.count - 2] : self.navigationController.topViewController;
  104. UIBarButtonItem *backItem = lastViewControllerInStack.navigationItem.backBarButtonItem ?: [UIBarButtonItem new];
  105. backItem.accessibilityIdentifier = testID;
  106. icon = color
  107. ? [[icon withTintColor:color] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]
  108. : icon;
  109. [self setBackIndicatorImage:icon withColor:color];
  110. if (showTitle) {
  111. backItem.title = title ? title : lastViewControllerInStack.navigationItem.title;
  112. } else {
  113. backItem.title = @"";
  114. }
  115. backItem.tintColor = color;
  116. if (fontFamily) {
  117. CGFloat resolvedFontSize = fontSize ? fontSize.floatValue : 17.0;
  118. [backItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys: [UIFont fontWithName:fontFamily size:resolvedFontSize], NSFontAttributeName, nil] forState:UIControlStateNormal];
  119. [backItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys: [UIFont fontWithName:fontFamily size:resolvedFontSize], NSFontAttributeName, nil] forState:UIControlStateHighlighted];
  120. }
  121. lastViewControllerInStack.navigationItem.backBarButtonItem = backItem;
  122. }
  123. - (BOOL)transparent {
  124. return self.backgroundColor.isTransparent;
  125. }
  126. @end