react-native-navigation的迁移库

TopBarPresenter.m 6.8KB

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