react-native-navigation的迁移库

RCCTabBarController.m 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. #import "RCCTabBarController.h"
  2. #import "RCCViewController.h"
  3. #import "RCTConvert.h"
  4. #import "RCCManager.h"
  5. @implementation RCCTabBarController
  6. - (UIImage *)image:(UIImage*)image withColor:(UIColor *)color1
  7. {
  8. UIGraphicsBeginImageContextWithOptions(image.size, NO, image.scale);
  9. CGContextRef context = UIGraphicsGetCurrentContext();
  10. CGContextTranslateCTM(context, 0, image.size.height);
  11. CGContextScaleCTM(context, 1.0, -1.0);
  12. CGContextSetBlendMode(context, kCGBlendModeNormal);
  13. CGRect rect = CGRectMake(0, 0, image.size.width, image.size.height);
  14. CGContextClipToMask(context, rect, image.CGImage);
  15. [color1 setFill];
  16. CGContextFillRect(context, rect);
  17. UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
  18. UIGraphicsEndImageContext();
  19. return newImage;
  20. }
  21. - (instancetype)initWithProps:(NSDictionary *)props children:(NSArray *)children globalProps:(NSDictionary*)globalProps bridge:(RCTBridge *)bridge
  22. {
  23. self = [super init];
  24. if (!self) return nil;
  25. self.tabBar.translucent = YES; // default
  26. UIColor *buttonColor = nil;
  27. UIColor *selectedButtonColor = nil;
  28. NSDictionary *tabsStyle = props[@"style"];
  29. if (tabsStyle)
  30. {
  31. NSString *tabBarButtonColor = tabsStyle[@"tabBarButtonColor"];
  32. if (tabBarButtonColor)
  33. {
  34. UIColor *color = tabBarButtonColor != (id)[NSNull null] ? [RCTConvert UIColor:tabBarButtonColor] : nil;
  35. self.tabBar.tintColor = color;
  36. buttonColor = color;
  37. selectedButtonColor = color;
  38. }
  39. NSString *tabBarSelectedButtonColor = tabsStyle[@"tabBarSelectedButtonColor"];
  40. if (tabBarSelectedButtonColor)
  41. {
  42. UIColor *color = tabBarSelectedButtonColor != (id)[NSNull null] ? [RCTConvert UIColor:tabBarSelectedButtonColor] : nil;
  43. self.tabBar.tintColor = color;
  44. selectedButtonColor = color;
  45. }
  46. NSString *tabBarBackgroundColor = tabsStyle[@"tabBarBackgroundColor"];
  47. if (tabBarBackgroundColor)
  48. {
  49. UIColor *color = tabBarBackgroundColor != (id)[NSNull null] ? [RCTConvert UIColor:tabBarBackgroundColor] : nil;
  50. self.tabBar.barTintColor = color;
  51. }
  52. }
  53. NSMutableArray *viewControllers = [NSMutableArray array];
  54. // go over all the tab bar items
  55. for (NSDictionary *tabItemLayout in children)
  56. {
  57. // make sure the layout is valid
  58. if (![tabItemLayout[@"type"] isEqualToString:@"TabBarControllerIOS.Item"]) continue;
  59. if (!tabItemLayout[@"props"]) continue;
  60. // get the view controller inside
  61. if (!tabItemLayout[@"children"]) continue;
  62. if (![tabItemLayout[@"children"] isKindOfClass:[NSArray class]]) continue;
  63. if ([tabItemLayout[@"children"] count] < 1) continue;
  64. NSDictionary *childLayout = tabItemLayout[@"children"][0];
  65. UIViewController *viewController = [RCCViewController controllerWithLayout:childLayout globalProps:globalProps bridge:bridge];
  66. if (!viewController) continue;
  67. // create the tab icon and title
  68. NSString *title = tabItemLayout[@"props"][@"title"];
  69. UIImage *iconImage = nil;
  70. id icon = tabItemLayout[@"props"][@"icon"];
  71. if (icon)
  72. {
  73. iconImage = [RCTConvert UIImage:icon];
  74. if (buttonColor)
  75. {
  76. iconImage = [[self image:iconImage withColor:buttonColor] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
  77. }
  78. }
  79. UIImage *iconImageSelected = nil;
  80. id selectedIcon = tabItemLayout[@"props"][@"selectedIcon"];
  81. if (selectedIcon) iconImageSelected = [RCTConvert UIImage:selectedIcon];
  82. viewController.tabBarItem = [[UITabBarItem alloc] initWithTitle:title image:iconImage tag:0];
  83. viewController.tabBarItem.accessibilityIdentifier = tabItemLayout[@"props"][@"testID"];
  84. viewController.tabBarItem.selectedImage = iconImageSelected;
  85. if (buttonColor)
  86. {
  87. [viewController.tabBarItem setTitleTextAttributes:
  88. @{NSForegroundColorAttributeName : buttonColor} forState:UIControlStateNormal];
  89. }
  90. if (selectedButtonColor)
  91. {
  92. [viewController.tabBarItem setTitleTextAttributes:
  93. @{NSForegroundColorAttributeName : selectedButtonColor} forState:UIControlStateSelected];
  94. }
  95. // create badge
  96. NSObject *badge = tabItemLayout[@"props"][@"badge"];
  97. if (badge == nil || [badge isEqual:[NSNull null]])
  98. {
  99. viewController.tabBarItem.badgeValue = nil;
  100. }
  101. else
  102. {
  103. viewController.tabBarItem.badgeValue = [NSString stringWithFormat:@"%@", badge];
  104. }
  105. [viewControllers addObject:viewController];
  106. }
  107. // replace the tabs
  108. self.viewControllers = viewControllers;
  109. return self;
  110. }
  111. - (void)performAction:(NSString*)performAction actionParams:(NSDictionary*)actionParams bridge:(RCTBridge *)bridge completion:(void (^)(void))completion
  112. {
  113. if ([performAction isEqualToString:@"setBadge"])
  114. {
  115. UIViewController *viewController = nil;
  116. NSNumber *tabIndex = actionParams[@"tabIndex"];
  117. if (tabIndex)
  118. {
  119. int i = (int)[tabIndex integerValue];
  120. if ([self.viewControllers count] > i)
  121. {
  122. viewController = [self.viewControllers objectAtIndex:i];
  123. }
  124. }
  125. NSString *contentId = actionParams[@"contentId"];
  126. NSString *contentType = actionParams[@"contentType"];
  127. if (contentId && contentType)
  128. {
  129. viewController = [[RCCManager sharedInstance] getControllerWithId:contentId componentType:contentType];
  130. }
  131. if (viewController)
  132. {
  133. NSObject *badge = actionParams[@"badge"];
  134. if (badge == nil || [badge isEqual:[NSNull null]])
  135. {
  136. viewController.tabBarItem.badgeValue = nil;
  137. }
  138. else
  139. {
  140. viewController.tabBarItem.badgeValue = [NSString stringWithFormat:@"%@", badge];
  141. }
  142. }
  143. }
  144. if ([performAction isEqualToString:@"switchTo"])
  145. {
  146. UIViewController *viewController = nil;
  147. NSNumber *tabIndex = actionParams[@"tabIndex"];
  148. if (tabIndex)
  149. {
  150. int i = (int)[tabIndex integerValue];
  151. if ([self.viewControllers count] > i)
  152. {
  153. viewController = [self.viewControllers objectAtIndex:i];
  154. }
  155. }
  156. NSString *contentId = actionParams[@"contentId"];
  157. NSString *contentType = actionParams[@"contentType"];
  158. if (contentId && contentType)
  159. {
  160. viewController = [[RCCManager sharedInstance] getControllerWithId:contentId componentType:contentType];
  161. }
  162. if (viewController)
  163. {
  164. [self setSelectedViewController:viewController];
  165. }
  166. }
  167. if ([performAction isEqualToString:@"setTabBarHidden"])
  168. {
  169. BOOL hidden = [actionParams[@"hidden"] boolValue];
  170. [UIView animateWithDuration: ([actionParams[@"animated"] boolValue] ? 0.45 : 0)
  171. delay: 0
  172. usingSpringWithDamping: 0.75
  173. initialSpringVelocity: 0
  174. options: (hidden ? UIViewAnimationOptionCurveEaseIn : UIViewAnimationOptionCurveEaseOut)
  175. animations:^()
  176. {
  177. self.tabBar.transform = hidden ? CGAffineTransformMakeTranslation(0, self.tabBar.frame.size.height) : CGAffineTransformIdentity;
  178. }
  179. completion:^(BOOL finished)
  180. {
  181. if (completion != nil)
  182. {
  183. completion();
  184. }
  185. }];
  186. return;
  187. }
  188. else if (completion != nil)
  189. {
  190. completion();
  191. }
  192. }
  193. @end