react-native-navigation的迁移库

RCCTabBarController.m 8.9KB

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