react-native-navigation的迁移库

RCCTabBarController.m 10KB

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