react-native-navigation的迁移库

RCCTabBarController.m 9.5KB

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