react-native-navigation的迁移库

RCCTabBarController.m 9.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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) {
  104. iconImageSelected = [RCTConvert UIImage:selectedIcon];
  105. } else {
  106. iconImageSelected = [RCTConvert UIImage:icon];
  107. }
  108. viewController.tabBarItem = [[UITabBarItem alloc] initWithTitle:title image:iconImage tag:0];
  109. viewController.tabBarItem.accessibilityIdentifier = tabItemLayout[@"props"][@"testID"];
  110. viewController.tabBarItem.selectedImage = iconImageSelected;
  111. NSMutableDictionary *unselectedAttributes = [RCTHelpers textAttributesFromDictionary:tabsStyle withPrefix:@"tabBarText" baseFont:[UIFont systemFontOfSize:10]];
  112. if (!unselectedAttributes[NSForegroundColorAttributeName] && buttonColor) {
  113. unselectedAttributes[NSForegroundColorAttributeName] = buttonColor;
  114. }
  115. [viewController.tabBarItem setTitleTextAttributes:unselectedAttributes forState:UIControlStateNormal]
  116. ;
  117. NSMutableDictionary *selectedAttributes = [RCTHelpers textAttributesFromDictionary:tabsStyle withPrefix:@"tabBarSelectedText" baseFont:[UIFont systemFontOfSize:10]];
  118. if (!selectedAttributes[NSForegroundColorAttributeName] && selectedButtonColor) {
  119. selectedAttributes[NSForegroundColorAttributeName] = selectedButtonColor;
  120. }
  121. [viewController.tabBarItem setTitleTextAttributes:selectedAttributes forState:UIControlStateSelected];
  122. // create badge
  123. NSObject *badge = tabItemLayout[@"props"][@"badge"];
  124. if (badge == nil || [badge isEqual:[NSNull null]])
  125. {
  126. viewController.tabBarItem.badgeValue = nil;
  127. }
  128. else
  129. {
  130. viewController.tabBarItem.badgeValue = [NSString stringWithFormat:@"%@", badge];
  131. }
  132. [viewControllers addObject:viewController];
  133. }
  134. // replace the tabs
  135. self.viewControllers = viewControllers;
  136. [self setRotation:props];
  137. return self;
  138. }
  139. - (void)performAction:(NSString*)performAction actionParams:(NSDictionary*)actionParams bridge:(RCTBridge *)bridge completion:(void (^)(void))completion
  140. {
  141. if ([performAction isEqualToString:@"setBadge"])
  142. {
  143. UIViewController *viewController = nil;
  144. NSNumber *tabIndex = actionParams[@"tabIndex"];
  145. if (tabIndex)
  146. {
  147. int i = (int)[tabIndex integerValue];
  148. if ([self.viewControllers count] > i)
  149. {
  150. viewController = [self.viewControllers objectAtIndex:i];
  151. }
  152. }
  153. NSString *contentId = actionParams[@"contentId"];
  154. NSString *contentType = actionParams[@"contentType"];
  155. if (contentId && contentType)
  156. {
  157. viewController = [[RCCManager sharedInstance] getControllerWithId:contentId componentType:contentType];
  158. }
  159. if (viewController)
  160. {
  161. NSObject *badge = actionParams[@"badge"];
  162. if (badge == nil || [badge isEqual:[NSNull null]])
  163. {
  164. viewController.tabBarItem.badgeValue = nil;
  165. }
  166. else
  167. {
  168. viewController.tabBarItem.badgeValue = [NSString stringWithFormat:@"%@", badge];
  169. }
  170. }
  171. }
  172. if ([performAction isEqualToString:@"switchTo"])
  173. {
  174. UIViewController *viewController = nil;
  175. NSNumber *tabIndex = actionParams[@"tabIndex"];
  176. if (tabIndex)
  177. {
  178. int i = (int)[tabIndex integerValue];
  179. if ([self.viewControllers count] > i)
  180. {
  181. viewController = [self.viewControllers objectAtIndex:i];
  182. }
  183. }
  184. NSString *contentId = actionParams[@"contentId"];
  185. NSString *contentType = actionParams[@"contentType"];
  186. if (contentId && contentType)
  187. {
  188. viewController = [[RCCManager sharedInstance] getControllerWithId:contentId componentType:contentType];
  189. }
  190. if (viewController)
  191. {
  192. [self setSelectedViewController:viewController];
  193. }
  194. }
  195. if ([performAction isEqualToString:@"setTabBarHidden"])
  196. {
  197. BOOL hidden = [actionParams[@"hidden"] boolValue];
  198. [UIView animateWithDuration: ([actionParams[@"animated"] boolValue] ? 0.45 : 0)
  199. delay: 0
  200. usingSpringWithDamping: 0.75
  201. initialSpringVelocity: 0
  202. options: (hidden ? UIViewAnimationOptionCurveEaseIn : UIViewAnimationOptionCurveEaseOut)
  203. animations:^()
  204. {
  205. self.tabBar.transform = hidden ? CGAffineTransformMakeTranslation(0, self.tabBar.frame.size.height) : CGAffineTransformIdentity;
  206. }
  207. completion:^(BOOL finished)
  208. {
  209. if (completion != nil)
  210. {
  211. completion();
  212. }
  213. }];
  214. return;
  215. }
  216. else if (completion != nil)
  217. {
  218. completion();
  219. }
  220. }
  221. +(void)sendScreenTabChangedEvent:(UIViewController*)viewController {
  222. if ([viewController.view isKindOfClass:[RCTRootView class]]){
  223. RCTRootView *rootView = (RCTRootView *)viewController.view;
  224. if (rootView.appProperties && rootView.appProperties[@"navigatorEventID"]) {
  225. NSString *navigatorID = rootView.appProperties[@"navigatorID"];
  226. NSString *screenInstanceID = rootView.appProperties[@"screenInstanceID"];
  227. [[[RCCManager sharedInstance] getBridge].eventDispatcher sendAppEventWithName:rootView.appProperties[@"navigatorEventID"] body:@
  228. {
  229. @"id": @"bottomTabSelected",
  230. @"navigatorID": navigatorID,
  231. @"screenInstanceID": screenInstanceID
  232. }];
  233. }
  234. }
  235. if ([viewController isKindOfClass:[UINavigationController class]]) {
  236. UINavigationController *navigationController = (UINavigationController*)viewController;
  237. UIViewController *topViewController = [navigationController topViewController];
  238. [RCCTabBarController sendScreenTabChangedEvent:topViewController];
  239. }
  240. }
  241. @end