react-native-navigation的迁移库

RCCTabBarController.m 9.6KB

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