react-native-navigation的迁移库

RCCTabBarController.m 7.9KB

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