react-native-navigation的迁移库

RCCViewController.m 37KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927
  1. #import "RCCViewController.h"
  2. #import "RCCNavigationController.h"
  3. #import "RCCTabBarController.h"
  4. #import "RCCDrawerController.h"
  5. #import "RCCTheSideBarManagerViewController.h"
  6. #import <React/RCTRootView.h>
  7. #import "RCCManager.h"
  8. #import <React/RCTConvert.h>
  9. #import <React/RCTEventDispatcher.h>
  10. #import "RCCExternalViewControllerProtocol.h"
  11. #import "RCTHelpers.h"
  12. #import "RCCTitleViewHelper.h"
  13. #import "RCCCustomTitleView.h"
  14. NSString* const RCCViewControllerCancelReactTouchesNotification = @"RCCViewControllerCancelReactTouchesNotification";
  15. const NSInteger BLUR_STATUS_TAG = 78264801;
  16. const NSInteger BLUR_NAVBAR_TAG = 78264802;
  17. const NSInteger TRANSPARENT_NAVBAR_TAG = 78264803;
  18. @interface RCCViewController() <UIGestureRecognizerDelegate, UIViewControllerPreviewingDelegate>
  19. @property (nonatomic) BOOL _hidesBottomBarWhenPushed;
  20. @property (nonatomic) BOOL _statusBarHideWithNavBar;
  21. @property (nonatomic) BOOL _statusBarHidden;
  22. @property (nonatomic) BOOL _statusBarTextColorSchemeLight;
  23. @property (nonatomic, strong) NSDictionary *originalNavBarImages;
  24. @property (nonatomic, strong) UIImageView *navBarHairlineImageView;
  25. @property (nonatomic, weak) id <UIGestureRecognizerDelegate> originalInteractivePopGestureDelegate;
  26. @end
  27. @implementation RCCViewController
  28. -(UIImageView *)navBarHairlineImageView {
  29. if (!_navBarHairlineImageView) {
  30. _navBarHairlineImageView = [self findHairlineImageViewUnder:self.navigationController.navigationBar];
  31. }
  32. return _navBarHairlineImageView;
  33. }
  34. + (UIViewController*)controllerWithLayout:(NSDictionary *)layout globalProps:(NSDictionary *)globalProps bridge:(RCTBridge *)bridge
  35. {
  36. UIViewController* controller = nil;
  37. if (!layout) return nil;
  38. // get props
  39. if (!layout[@"props"]) return nil;
  40. if (![layout[@"props"] isKindOfClass:[NSDictionary class]]) return nil;
  41. NSDictionary *props = layout[@"props"];
  42. // get children
  43. if (!layout[@"children"]) return nil;
  44. if (![layout[@"children"] isKindOfClass:[NSArray class]]) return nil;
  45. NSArray *children = layout[@"children"];
  46. // create according to type
  47. NSString *type = layout[@"type"];
  48. if (!type) return nil;
  49. // regular view controller
  50. if ([type isEqualToString:@"ViewControllerIOS"])
  51. {
  52. controller = [[RCCViewController alloc] initWithProps:props children:children globalProps:globalProps bridge:bridge];
  53. }
  54. // navigation controller
  55. if ([type isEqualToString:@"NavigationControllerIOS"])
  56. {
  57. controller = [[RCCNavigationController alloc] initWithProps:props children:children globalProps:globalProps bridge:bridge];
  58. }
  59. // tab bar controller
  60. if ([type isEqualToString:@"TabBarControllerIOS"])
  61. {
  62. controller = [[RCCTabBarController alloc] initWithProps:props children:children globalProps:globalProps bridge:bridge];
  63. }
  64. // side menu controller
  65. if ([type isEqualToString:@"DrawerControllerIOS"])
  66. {
  67. NSString *drawerType = props[@"type"];
  68. if ([drawerType isEqualToString:@"TheSideBar"]) {
  69. controller = [[RCCTheSideBarManagerViewController alloc] initWithProps:props children:children globalProps:globalProps bridge:bridge];
  70. }
  71. else {
  72. controller = [[RCCDrawerController alloc] initWithProps:props children:children globalProps:globalProps bridge:bridge];
  73. }
  74. }
  75. // register the controller if we have an id
  76. NSString *componentId = props[@"id"];
  77. if (controller && componentId)
  78. {
  79. [[RCCManager sharedInstance] registerController:controller componentId:componentId componentType:type];
  80. if([controller isKindOfClass:[RCCViewController class]])
  81. {
  82. ((RCCViewController*)controller).controllerId = componentId;
  83. }
  84. }
  85. // set background image at root level
  86. NSString *rootBackgroundImageName = props[@"style"][@"rootBackgroundImageName"];
  87. if (rootBackgroundImageName) {
  88. UIImage *image = [UIImage imageNamed: rootBackgroundImageName];
  89. UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
  90. [controller.view insertSubview:imageView atIndex:0];
  91. }
  92. return controller;
  93. }
  94. -(NSDictionary*)addCommandTypeAndTimestampIfExists:(NSDictionary*)globalProps passProps:(NSDictionary*)passProps {
  95. NSMutableDictionary *modifiedPassProps = [NSMutableDictionary dictionaryWithDictionary:passProps];
  96. NSString *commandType = globalProps[GLOBAL_SCREEN_ACTION_COMMAND_TYPE];
  97. if (commandType) {
  98. modifiedPassProps[GLOBAL_SCREEN_ACTION_COMMAND_TYPE] = commandType;
  99. }
  100. NSString *timestamp = globalProps[GLOBAL_SCREEN_ACTION_TIMESTAMP];
  101. if (timestamp) {
  102. modifiedPassProps[GLOBAL_SCREEN_ACTION_TIMESTAMP] = timestamp;
  103. }
  104. return modifiedPassProps;
  105. }
  106. - (instancetype)initWithProps:(NSDictionary *)props children:(NSArray *)children globalProps:(NSDictionary *)globalProps bridge:(RCTBridge *)bridge
  107. {
  108. NSString *component = props[@"component"];
  109. if (!component) return nil;
  110. NSDictionary *passProps = props[@"passProps"];
  111. NSDictionary *navigatorStyle = props[@"style"];
  112. NSMutableDictionary *mergedProps = [NSMutableDictionary dictionaryWithDictionary:globalProps];
  113. [mergedProps addEntriesFromDictionary:passProps];
  114. RCTRootView *reactView = [[RCTRootView alloc] initWithBridge:bridge moduleName:component initialProperties:mergedProps];
  115. if (!reactView) return nil;
  116. self = [super init];
  117. if (!self) return nil;
  118. [self commonInit:reactView navigatorStyle:navigatorStyle props:props];
  119. self.navigationController.interactivePopGestureRecognizer.delegate = self;
  120. return self;
  121. }
  122. - (instancetype)initWithComponent:(NSString *)component passProps:(NSDictionary *)passProps navigatorStyle:(NSDictionary*)navigatorStyle globalProps:(NSDictionary *)globalProps bridge:(RCTBridge *)bridge
  123. {
  124. NSMutableDictionary *mergedProps = [NSMutableDictionary dictionaryWithDictionary:globalProps];
  125. [mergedProps addEntriesFromDictionary:passProps];
  126. RCTRootView *reactView = [[RCTRootView alloc] initWithBridge:bridge moduleName:component initialProperties:mergedProps];
  127. if (!reactView) return nil;
  128. self = [super init];
  129. if (!self) return nil;
  130. NSDictionary *modifiedPassProps = [self addCommandTypeAndTimestampIfExists:globalProps passProps:passProps];
  131. [self commonInit:reactView navigatorStyle:navigatorStyle props:modifiedPassProps];
  132. return self;
  133. }
  134. - (void)commonInit:(RCTRootView*)reactView navigatorStyle:(NSDictionary*)navigatorStyle props:(NSDictionary*)props
  135. {
  136. self.view = reactView;
  137. self.edgesForExtendedLayout = UIRectEdgeNone; // default
  138. self.automaticallyAdjustsScrollViewInsets = NO; // default
  139. self.navigatorStyle = [NSMutableDictionary dictionaryWithDictionary:[[RCCManager sharedInstance] getAppStyle]];
  140. [self.navigatorStyle addEntriesFromDictionary:navigatorStyle];
  141. [self setStyleOnInit];
  142. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onRNReload) name:RCTJavaScriptWillStartLoadingNotification object:nil];
  143. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onCancelReactTouches) name:RCCViewControllerCancelReactTouchesNotification object:nil];
  144. self.commandType = props[GLOBAL_SCREEN_ACTION_COMMAND_TYPE];
  145. self.timestamp = props[GLOBAL_SCREEN_ACTION_TIMESTAMP];
  146. // In order to support 3rd party native ViewControllers, we support passing a class name as a prop named `ExternalNativeScreenClass`
  147. // In this case, we create an instance and add it as a child ViewController which preserves the VC lifecycle.
  148. // In case some props are necessary in the native ViewController, the ExternalNativeScreenProps can be used to pass them
  149. [self addExternalVCIfNecessary:props];
  150. }
  151. - (void)dealloc
  152. {
  153. [[NSNotificationCenter defaultCenter] removeObserver:self];
  154. self.view = nil;
  155. }
  156. -(void)onRNReload
  157. {
  158. [[NSNotificationCenter defaultCenter] removeObserver:self];
  159. self.view = nil;
  160. }
  161. -(void)onCancelReactTouches
  162. {
  163. if ([self.view isKindOfClass:[RCTRootView class]]){
  164. [(RCTRootView*)self.view cancelTouches];
  165. }
  166. }
  167. - (void)sendScreenChangedEvent:(NSString *)eventName
  168. {
  169. if ([self.view isKindOfClass:[RCTRootView class]]){
  170. RCTRootView *rootView = (RCTRootView *)self.view;
  171. if (rootView.appProperties && rootView.appProperties[@"navigatorEventID"]) {
  172. [[[RCCManager sharedInstance] getBridge].eventDispatcher sendAppEventWithName:rootView.appProperties[@"navigatorEventID"] body:@
  173. {
  174. @"type": @"ScreenChangedEvent",
  175. @"id": eventName
  176. }];
  177. }
  178. }
  179. }
  180. - (void)sendGlobalScreenEvent:(NSString *)eventName endTimestampString:(NSString *)endTimestampStr shouldReset:(BOOL)shouldReset {
  181. if (!self.commandType) return;
  182. if ([self.view isKindOfClass:[RCTRootView class]]){
  183. NSString *screenName = [((RCTRootView*)self.view) moduleName];
  184. [[[RCCManager sharedInstance] getBridge].eventDispatcher sendAppEventWithName:eventName body:@
  185. {
  186. @"commandType": self.commandType ? self.commandType : @"",
  187. @"screen": screenName ? screenName : @"",
  188. @"startTime": self.timestamp ? self.timestamp : @"",
  189. @"endTime": endTimestampStr ? endTimestampStr : @""
  190. }];
  191. if (shouldReset) {
  192. self.commandType = nil;
  193. self.timestamp = nil;
  194. }
  195. }
  196. }
  197. -(BOOL)isDisappearTriggeredFromPop:(NSString *)eventName {
  198. NSArray *navigationViewControllers = self.navigationController.viewControllers;
  199. if (navigationViewControllers.lastObject == self || [navigationViewControllers indexOfObject:self] == NSNotFound) {
  200. return YES;
  201. }
  202. return NO;
  203. }
  204. - (NSString *)getTimestampString {
  205. long long milliseconds = (long long)([[NSDate date] timeIntervalSince1970] * 1000.0);
  206. return [NSString stringWithFormat:@"%lld", milliseconds];
  207. }
  208. // This is walk around for React-Native bug.
  209. // https://github.com/wix/react-native-navigation/issues/1446
  210. //
  211. // Buttons in ScrollView after changing route/pushing/showing modal
  212. // while there is a momentum scroll are not clickable.
  213. // Back to normal after user start scroll with momentum
  214. - (void)_traverseAndCall:(UIView*)view
  215. {
  216. if([view isKindOfClass:[UIScrollView class]] && ([[(UIScrollView*)view delegate] respondsToSelector:@selector(scrollViewDidEndDecelerating:)]) ) {
  217. dispatch_async(dispatch_get_main_queue(), ^{
  218. [[(UIScrollView*)view delegate] scrollViewDidEndDecelerating:(id)view];
  219. });
  220. }
  221. [view.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  222. [self _traverseAndCall:obj];
  223. }];
  224. }
  225. - (void)viewDidAppear:(BOOL)animated
  226. {
  227. [super viewDidAppear:animated];
  228. [self sendGlobalScreenEvent:@"didAppear" endTimestampString:[self getTimestampString] shouldReset:YES];
  229. [self sendScreenChangedEvent:@"didAppear"];
  230. }
  231. - (void)viewWillAppear:(BOOL)animated
  232. {
  233. [super viewWillAppear:animated];
  234. [self sendGlobalScreenEvent:@"willAppear" endTimestampString:[self getTimestampString] shouldReset:NO];
  235. [self sendScreenChangedEvent:@"willAppear"];
  236. [self setStyleOnAppear];
  237. }
  238. - (void)viewDidDisappear:(BOOL)animated
  239. {
  240. [self _traverseAndCall:self.view];
  241. [super viewDidDisappear:animated];
  242. [self sendGlobalScreenEvent:@"didDisappear" endTimestampString:[self getTimestampString] shouldReset:YES];
  243. [self sendScreenChangedEvent:@"didDisappear"];
  244. }
  245. - (void)viewWillDisappear:(BOOL)animated
  246. {
  247. [super viewWillDisappear:animated];
  248. [self sendGlobalScreenEvent:@"willDisappear" endTimestampString:[self getTimestampString] shouldReset:NO];
  249. [self sendScreenChangedEvent:@"willDisappear"];
  250. [self setStyleOnDisappear];
  251. }
  252. // most styles should be set here since when we pop a view controller that changed them
  253. // we want to reset the style to what we expect (so we need to reset on every willAppear)
  254. - (void)setStyleOnAppear
  255. {
  256. [self setStyleOnAppearForViewController:self appeared:false];
  257. }
  258. - (void)updateStyle
  259. {
  260. [self setStyleOnAppearForViewController:self appeared:true];
  261. }
  262. -(void)setStyleOnAppearForViewController:(UIViewController*)viewController appeared:(BOOL)appeared
  263. {
  264. NSString *screenBackgroundColor = self.navigatorStyle[@"screenBackgroundColor"];
  265. if (screenBackgroundColor) {
  266. UIColor *color = screenBackgroundColor != (id)[NSNull null] ? [RCTConvert UIColor:screenBackgroundColor] : nil;
  267. viewController.view.backgroundColor = color;
  268. }
  269. NSString *screenBackgroundImageName = self.navigatorStyle[@"screenBackgroundImageName"];
  270. if (screenBackgroundImageName) {
  271. UIImage *image = [UIImage imageNamed: screenBackgroundImageName];
  272. viewController.view.layer.contents = (__bridge id _Nullable)(image.CGImage);
  273. }
  274. NSString *navBarBackgroundColor = self.navigatorStyle[@"navBarBackgroundColor"];
  275. if (navBarBackgroundColor) {
  276. UIColor *color = navBarBackgroundColor != (id)[NSNull null] ? [RCTConvert UIColor:navBarBackgroundColor] : nil;
  277. viewController.navigationController.navigationBar.barTintColor = color;
  278. } else {
  279. viewController.navigationController.navigationBar.barTintColor = nil;
  280. }
  281. NSMutableDictionary *titleTextAttributes = [RCTHelpers textAttributesFromDictionary:self.navigatorStyle withPrefix:@"navBarText" baseFont:[UIFont boldSystemFontOfSize:17]];
  282. [self.navigationController.navigationBar setTitleTextAttributes:titleTextAttributes];
  283. NSMutableDictionary *navButtonTextAttributes = [RCTHelpers textAttributesFromDictionary:self.navigatorStyle withPrefix:@"navBarButton"];
  284. NSMutableDictionary *leftNavButtonTextAttributes = [RCTHelpers textAttributesFromDictionary:self.navigatorStyle withPrefix:@"navBarLeftButton"];
  285. NSMutableDictionary *rightNavButtonTextAttributes = [RCTHelpers textAttributesFromDictionary:self.navigatorStyle withPrefix:@"navBarRightButton"];
  286. if (
  287. navButtonTextAttributes.allKeys.count > 0 ||
  288. leftNavButtonTextAttributes.allKeys.count > 0 ||
  289. rightNavButtonTextAttributes.allKeys.count > 0
  290. ) {
  291. for (UIBarButtonItem *item in viewController.navigationItem.leftBarButtonItems) {
  292. if (leftNavButtonTextAttributes.allKeys.count > 0) {
  293. NSDictionary *previousAttributes = [item titleTextAttributesForState:UIControlStateNormal];
  294. NSMutableDictionary *mergedAttributes;
  295. if (leftNavButtonTextAttributes.allKeys.count > 0) {
  296. mergedAttributes = [leftNavButtonTextAttributes mutableCopy];
  297. } else {
  298. mergedAttributes = [navButtonTextAttributes mutableCopy];
  299. }
  300. [mergedAttributes addEntriesFromDictionary:previousAttributes];
  301. [item setTitleTextAttributes:[mergedAttributes copy] forState:UIControlStateNormal];
  302. [item setTitleTextAttributes:[mergedAttributes copy] forState:UIControlStateHighlighted];
  303. }
  304. }
  305. for (UIBarButtonItem *item in viewController.navigationItem.rightBarButtonItems) {
  306. NSDictionary *previousAttributes = [item titleTextAttributesForState:UIControlStateNormal];
  307. NSMutableDictionary *mergedAttributes;
  308. if (rightNavButtonTextAttributes.allKeys.count > 0) {
  309. mergedAttributes = [rightNavButtonTextAttributes mutableCopy];
  310. } else {
  311. mergedAttributes = [navButtonTextAttributes mutableCopy];
  312. }
  313. [mergedAttributes addEntriesFromDictionary:previousAttributes];
  314. [item setTitleTextAttributes:[mergedAttributes copy] forState:UIControlStateNormal];
  315. [item setTitleTextAttributes:[mergedAttributes copy] forState:UIControlStateHighlighted];
  316. }
  317. // At the moment, this seems to be the only thing that gets the back button correctly
  318. [navButtonTextAttributes removeObjectForKey:NSForegroundColorAttributeName];
  319. [[UIBarButtonItem appearance] setTitleTextAttributes:navButtonTextAttributes forState:UIControlStateNormal];
  320. [[UIBarButtonItem appearance] setTitleTextAttributes:navButtonTextAttributes forState:UIControlStateHighlighted];
  321. }
  322. NSString *navBarButtonColor = self.navigatorStyle[@"navBarButtonColor"];
  323. if (navBarButtonColor) {
  324. UIColor *color = navBarButtonColor != (id)[NSNull null] ? [RCTConvert UIColor:navBarButtonColor] : nil;
  325. viewController.navigationController.navigationBar.tintColor = color;
  326. } else
  327. {
  328. viewController.navigationController.navigationBar.tintColor = nil;
  329. }
  330. BOOL topBarElevationShadowEnabled = self.navigatorStyle[@"topBarElevationShadowEnabled"] != (id)[NSNull null] ? [RCTConvert CGFloat:self.navigatorStyle[@"topBarElevationShadowEnabled"]] : NO;
  331. if (topBarElevationShadowEnabled) {
  332. CGFloat shadowOpacity = self.navigatorStyle[@"topBarShadowOpacity"] != 0 ? [RCTConvert CGFloat:self.navigatorStyle[@"topBarShadowOpacity"]] : 0.2;
  333. CGFloat shadowOffset = self.navigatorStyle[@"topBarShadowOffset"] != 0 ? [RCTConvert CGFloat:self.navigatorStyle[@"topBarShadowOffset"]] : 3.0;
  334. CGFloat shadowRadius = self.navigatorStyle[@"topBarShadowRadius"] != 0 ? [RCTConvert CGFloat:self.navigatorStyle[@"topBarShadowRadius"]] : 2.0;
  335. UIColor *shadowColor = self.navigatorStyle[@"topBarShadowColor"] != (id)[NSNull null] ? [RCTConvert UIColor:self.navigatorStyle[@"topBarShadowColor"]] : UIColor.blackColor;
  336. viewController.navigationController.navigationBar.layer.shadowOpacity = shadowOpacity;
  337. viewController.navigationController.navigationBar.layer.shadowColor = shadowColor.CGColor;
  338. viewController.navigationController.navigationBar.layer.shadowOffset = CGSizeMake(0, shadowOffset);
  339. viewController.navigationController.navigationBar.layer.shadowRadius = shadowRadius;
  340. }
  341. BOOL viewControllerBasedStatusBar = false;
  342. NSObject *viewControllerBasedStatusBarAppearance = [[NSBundle mainBundle] infoDictionary][@"UIViewControllerBasedStatusBarAppearance"];
  343. if (viewControllerBasedStatusBarAppearance && [viewControllerBasedStatusBarAppearance isKindOfClass:[NSNumber class]]) {
  344. viewControllerBasedStatusBar = [(NSNumber *)viewControllerBasedStatusBarAppearance boolValue];
  345. }
  346. NSString *statusBarTextColorSchemeSingleScreen = self.navigatorStyle[@"statusBarTextColorSchemeSingleScreen"];
  347. NSString *statusBarTextColorScheme = self.navigatorStyle[@"statusBarTextColorScheme"];
  348. NSString *finalColorScheme = statusBarTextColorSchemeSingleScreen ? : statusBarTextColorScheme;
  349. if (finalColorScheme && [finalColorScheme isEqualToString:@"light"]) {
  350. if (!statusBarTextColorSchemeSingleScreen) {
  351. viewController.navigationController.navigationBar.barStyle = UIBarStyleBlack;
  352. }
  353. self._statusBarTextColorSchemeLight = true;
  354. if (!viewControllerBasedStatusBarAppearance) {
  355. [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
  356. }
  357. [viewController setNeedsStatusBarAppearanceUpdate];
  358. } else {
  359. if (!statusBarTextColorSchemeSingleScreen) {
  360. viewController.navigationController.navigationBar.barStyle = UIBarStyleDefault;
  361. }
  362. self._statusBarTextColorSchemeLight = false;
  363. if (!viewControllerBasedStatusBarAppearance) {
  364. [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
  365. }
  366. [viewController setNeedsStatusBarAppearanceUpdate];
  367. }
  368. NSNumber *tabBarHidden = self.navigatorStyle[@"tabBarHidden"];
  369. BOOL tabBarHiddenBool = tabBarHidden ? [tabBarHidden boolValue] : NO;
  370. if (tabBarHiddenBool) {
  371. UITabBar *tabBar = viewController.tabBarController.tabBar;
  372. tabBar.transform = CGAffineTransformMakeTranslation(0, tabBar.frame.size.height);
  373. }
  374. NSNumber *navBarHidden = self.navigatorStyle[@"navBarHidden"];
  375. BOOL navBarHiddenBool = navBarHidden ? [navBarHidden boolValue] : NO;
  376. if (viewController.navigationController.navigationBarHidden != navBarHiddenBool) {
  377. [viewController.navigationController setNavigationBarHidden:navBarHiddenBool animated:YES];
  378. }
  379. NSNumber *navBarHideOnScroll = self.navigatorStyle[@"navBarHideOnScroll"];
  380. BOOL navBarHideOnScrollBool = navBarHideOnScroll ? [navBarHideOnScroll boolValue] : NO;
  381. if (navBarHideOnScrollBool) {
  382. viewController.navigationController.hidesBarsOnSwipe = YES;
  383. } else {
  384. viewController.navigationController.hidesBarsOnSwipe = NO;
  385. }
  386. NSNumber *statusBarBlur = self.navigatorStyle[@"statusBarBlur"];
  387. BOOL statusBarBlurBool = statusBarBlur ? [statusBarBlur boolValue] : NO;
  388. if (statusBarBlurBool && ![viewController.view viewWithTag:BLUR_STATUS_TAG]) {
  389. UIVisualEffectView *blur = [[UIVisualEffectView alloc] initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleLight]];
  390. blur.frame = [[UIApplication sharedApplication] statusBarFrame];
  391. blur.tag = BLUR_STATUS_TAG;
  392. [viewController.view insertSubview:blur atIndex:0];
  393. }
  394. NSNumber *navBarBlur = self.navigatorStyle[@"navBarBlur"];
  395. BOOL navBarBlurBool = navBarBlur ? [navBarBlur boolValue] : NO;
  396. if (navBarBlurBool) {
  397. if (![viewController.navigationController.navigationBar viewWithTag:BLUR_NAVBAR_TAG]) {
  398. [self storeOriginalNavBarImages];
  399. [viewController.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
  400. viewController.navigationController.navigationBar.shadowImage = [UIImage new];
  401. UIVisualEffectView *blur = [[UIVisualEffectView alloc] initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleLight]];
  402. CGRect statusBarFrame = [[UIApplication sharedApplication] statusBarFrame];
  403. blur.frame = CGRectMake(0, -1 * statusBarFrame.size.height, viewController.navigationController.navigationBar.frame.size.width, viewController.navigationController.navigationBar.frame.size.height + statusBarFrame.size.height);
  404. blur.userInteractionEnabled = NO;
  405. blur.tag = BLUR_NAVBAR_TAG;
  406. [viewController.navigationController.navigationBar insertSubview:blur atIndex:0];
  407. [viewController.navigationController.navigationBar sendSubviewToBack:blur];
  408. }
  409. } else {
  410. UIView *blur = [viewController.navigationController.navigationBar viewWithTag:BLUR_NAVBAR_TAG];
  411. if (blur) {
  412. [blur removeFromSuperview];
  413. [viewController.navigationController.navigationBar setBackgroundImage:self.originalNavBarImages[@"bgImage"] forBarMetrics:UIBarMetricsDefault];
  414. viewController.navigationController.navigationBar.shadowImage = self.originalNavBarImages[@"shadowImage"];
  415. self.originalNavBarImages = nil;
  416. }
  417. }
  418. NSNumber *navBarTransparent = self.navigatorStyle[@"navBarTransparent"];
  419. BOOL navBarTransparentBool = navBarTransparent ? [navBarTransparent boolValue] : NO;
  420. void (^action)() = ^ {
  421. if (navBarTransparentBool)
  422. {
  423. if (![viewController.navigationController.navigationBar viewWithTag:TRANSPARENT_NAVBAR_TAG])
  424. {
  425. [self storeOriginalNavBarImages];
  426. [viewController.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
  427. viewController.navigationController.navigationBar.shadowImage = [UIImage new];
  428. UIView *transparentView = [[UIView alloc] initWithFrame:CGRectZero];
  429. transparentView.tag = TRANSPARENT_NAVBAR_TAG;
  430. [viewController.navigationController.navigationBar insertSubview:transparentView atIndex:0];
  431. }
  432. }
  433. else
  434. {
  435. UIView *transparentView = [viewController.navigationController.navigationBar viewWithTag:TRANSPARENT_NAVBAR_TAG];
  436. if (transparentView)
  437. {
  438. [transparentView removeFromSuperview];
  439. [viewController.navigationController.navigationBar setBackgroundImage:self.originalNavBarImages[@"bgImage"] forBarMetrics:UIBarMetricsDefault];
  440. viewController.navigationController.navigationBar.shadowImage = self.originalNavBarImages[@"shadowImage"];
  441. self.originalNavBarImages = nil;
  442. }
  443. }
  444. };
  445. if (!self.transitionCoordinator || self.transitionCoordinator.initiallyInteractive || !navBarTransparentBool || appeared) {
  446. action();
  447. } else {
  448. UIView* backgroundView = [self.navigationController.navigationBar valueForKey:@"backgroundView"];
  449. CGFloat originalAlpha = backgroundView.alpha;
  450. backgroundView.alpha = navBarTransparentBool ? 0.0 : 1.0;
  451. [self.transitionCoordinator animateAlongsideTransition:nil completion:^(id<UIViewControllerTransitionCoordinatorContext> _Nonnull context) {
  452. action();
  453. backgroundView.alpha = originalAlpha;
  454. }];
  455. }
  456. NSNumber *autoAdjustsScrollViewInsets = self.navigatorStyle[@"autoAdjustScrollViewInsets"];
  457. viewController.automaticallyAdjustsScrollViewInsets = autoAdjustsScrollViewInsets ? [autoAdjustsScrollViewInsets boolValue] : false;
  458. NSNumber *navBarTranslucent = self.navigatorStyle[@"navBarTranslucent"];
  459. BOOL navBarTranslucentBool = navBarTranslucent ? [navBarTranslucent boolValue] : NO;
  460. if (navBarTranslucentBool || navBarBlurBool) {
  461. viewController.navigationController.navigationBar.translucent = YES;
  462. } else {
  463. viewController.navigationController.navigationBar.translucent = NO;
  464. }
  465. NSNumber *extendedLayoutIncludesOpaqueBars = self.navigatorStyle[@"extendedLayoutIncludesOpaqueBars"];
  466. BOOL extendedLayoutIncludesOpaqueBarsBool = extendedLayoutIncludesOpaqueBars ? [extendedLayoutIncludesOpaqueBars boolValue] : NO;
  467. viewController.extendedLayoutIncludesOpaqueBars = extendedLayoutIncludesOpaqueBarsBool;
  468. NSNumber *drawUnderNavBar = self.navigatorStyle[@"drawUnderNavBar"];
  469. BOOL drawUnderNavBarBool = drawUnderNavBar ? [drawUnderNavBar boolValue] : NO;
  470. if (drawUnderNavBarBool) {
  471. viewController.edgesForExtendedLayout |= UIRectEdgeTop;
  472. }
  473. else {
  474. viewController.edgesForExtendedLayout &= ~UIRectEdgeTop;
  475. }
  476. NSNumber *drawUnderTabBar = self.navigatorStyle[@"drawUnderTabBar"];
  477. BOOL drawUnderTabBarBool = drawUnderTabBar ? [drawUnderTabBar boolValue] : NO;
  478. if (drawUnderTabBarBool) {
  479. viewController.edgesForExtendedLayout |= UIRectEdgeBottom;
  480. } else {
  481. viewController.edgesForExtendedLayout &= ~UIRectEdgeBottom;
  482. }
  483. NSNumber *removeNavBarBorder = self.navigatorStyle[@"navBarNoBorder"];
  484. BOOL removeNavBarBorderBool = removeNavBarBorder ? [removeNavBarBorder boolValue] : NO;
  485. if (removeNavBarBorderBool) {
  486. self.navBarHairlineImageView.hidden = YES;
  487. } else {
  488. self.navBarHairlineImageView.hidden = NO;
  489. }
  490. //Bug fix: in case there is a interactivePopGestureRecognizer, it prevents react-native from getting touch events on the left screen area that the gesture handles
  491. //overriding the delegate of the gesture prevents this from happening while keeping the gesture intact (another option was to disable it completely by demand)
  492. if(self.navigationController.viewControllers.count > 1){
  493. if (self.navigationController != nil && self.navigationController.interactivePopGestureRecognizer != nil)
  494. {
  495. id <UIGestureRecognizerDelegate> interactivePopGestureRecognizer = self.navigationController.interactivePopGestureRecognizer.delegate;
  496. if (interactivePopGestureRecognizer != nil && interactivePopGestureRecognizer != self)
  497. {
  498. self.originalInteractivePopGestureDelegate = interactivePopGestureRecognizer;
  499. self.navigationController.interactivePopGestureRecognizer.delegate = self;
  500. }
  501. }
  502. }
  503. NSString *navBarCustomView = self.navigatorStyle[@"navBarCustomView"];
  504. if (navBarCustomView && ![self.navigationItem.titleView isKindOfClass:[RCCCustomTitleView class]]) {
  505. if ([self.view isKindOfClass:[RCTRootView class]]) {
  506. RCTBridge *bridge = ((RCTRootView*)self.view).bridge;
  507. NSDictionary *initialProps = self.navigatorStyle[@"navBarCustomViewInitialProps"];
  508. RCTRootView *reactView = [[RCTRootView alloc] initWithBridge:bridge moduleName:navBarCustomView initialProperties:initialProps];
  509. reactView.sizeFlexibility = RCTRootViewSizeFlexibilityWidthAndHeight;
  510. RCCCustomTitleView *titleView = [[RCCCustomTitleView alloc] initWithFrame:self.navigationController.navigationBar.bounds
  511. subView:reactView
  512. alignment:self.navigatorStyle[@"navBarComponentAlignment"]];
  513. self.navigationItem.titleView = titleView;
  514. self.navigationItem.titleView.backgroundColor = [UIColor clearColor];
  515. self.navigationItem.titleView.clipsToBounds = YES;
  516. }
  517. }
  518. #if __IPHONE_OS_VERSION_MAX_ALLOWED > __IPHONE_10_3
  519. if (@available(iOS 11.0, *)) {
  520. if ([self.navigationController.navigationBar respondsToSelector:@selector(setPrefersLargeTitles:)]) {
  521. NSNumber *prefersLargeTitles = self.navigatorStyle[@"largeTitle"];
  522. if (prefersLargeTitles) {
  523. if ([prefersLargeTitles boolValue]) {
  524. self.navigationController.navigationBar.prefersLargeTitles = YES;
  525. self.navigationItem.largeTitleDisplayMode = UINavigationItemLargeTitleDisplayModeAlways;
  526. self.navigationItem.titleView = nil;
  527. } else {
  528. self.navigationItem.largeTitleDisplayMode = UINavigationItemLargeTitleDisplayModeNever;
  529. }
  530. } else {
  531. self.navigationController.navigationBar.prefersLargeTitles = NO;
  532. self.navigationItem.largeTitleDisplayMode = UINavigationItemLargeTitleDisplayModeNever;
  533. }
  534. }
  535. }
  536. #endif
  537. }
  538. - (void) viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {
  539. [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
  540. RCCCustomTitleView* customNavBar = (RCCCustomTitleView*) self.navigationItem.titleView;
  541. if (customNavBar && [customNavBar isKindOfClass:[RCCCustomTitleView class]]) {
  542. [customNavBar viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
  543. }
  544. }
  545. -(void)storeOriginalNavBarImages {
  546. NSMutableDictionary *originalNavBarImages = [@{} mutableCopy];
  547. UIImage *bgImage = [self.navigationController.navigationBar backgroundImageForBarMetrics:UIBarMetricsDefault];
  548. if (bgImage != nil) {
  549. originalNavBarImages[@"bgImage"] = bgImage;
  550. }
  551. UIImage *shadowImage = self.navigationController.navigationBar.shadowImage;
  552. if (shadowImage != nil) {
  553. originalNavBarImages[@"shadowImage"] = shadowImage;
  554. }
  555. self.originalNavBarImages = originalNavBarImages;
  556. }
  557. -(void)setStyleOnDisappear {
  558. self.navBarHairlineImageView.hidden = NO;
  559. if (self.navigationController != nil && self.navigationController.interactivePopGestureRecognizer != nil && self.originalInteractivePopGestureDelegate != nil)
  560. {
  561. self.navigationController.interactivePopGestureRecognizer.delegate = self.originalInteractivePopGestureDelegate;
  562. self.originalInteractivePopGestureDelegate = nil;
  563. }
  564. }
  565. // only styles that can't be set on willAppear should be set here
  566. - (void)setStyleOnInit
  567. {
  568. NSNumber *tabBarHidden = self.navigatorStyle[@"tabBarHidden"];
  569. BOOL tabBarHiddenBool = tabBarHidden ? [tabBarHidden boolValue] : NO;
  570. if (tabBarHiddenBool) {
  571. self._hidesBottomBarWhenPushed = YES;
  572. } else {
  573. self._hidesBottomBarWhenPushed = NO;
  574. }
  575. NSNumber *statusBarHideWithNavBar = self.navigatorStyle[@"statusBarHideWithNavBar"];
  576. BOOL statusBarHideWithNavBarBool = statusBarHideWithNavBar ? [statusBarHideWithNavBar boolValue] : NO;
  577. if (statusBarHideWithNavBarBool) {
  578. self._statusBarHideWithNavBar = YES;
  579. } else {
  580. self._statusBarHideWithNavBar = NO;
  581. }
  582. NSNumber *statusBarHidden = self.navigatorStyle[@"statusBarHidden"];
  583. BOOL statusBarHiddenBool = statusBarHidden ? [statusBarHidden boolValue] : NO;
  584. if (statusBarHiddenBool) {
  585. self._statusBarHidden = YES;
  586. } else {
  587. self._statusBarHidden = NO;
  588. }
  589. NSDictionary *preferredContentSize = self.navigatorStyle[@"preferredContentSize"];
  590. if (preferredContentSize) {
  591. NSNumber *width = preferredContentSize[@"width"];
  592. NSNumber *height = preferredContentSize[@"height"];
  593. if (width && height) {
  594. self.preferredContentSize = CGSizeMake([width floatValue], [height floatValue]);
  595. }
  596. }
  597. }
  598. - (BOOL)hidesBottomBarWhenPushed
  599. {
  600. if (!self._hidesBottomBarWhenPushed) return NO;
  601. return (self.navigationController.topViewController == self) && ![(RCCTabBarController*)self.tabBarController tabBarHidden];
  602. }
  603. - (BOOL)prefersStatusBarHidden
  604. {
  605. if (self._statusBarHidden) {
  606. return YES;
  607. }
  608. if (self._statusBarHideWithNavBar) {
  609. return self.navigationController.isNavigationBarHidden;
  610. } else {
  611. return NO;
  612. }
  613. }
  614. - (void)setNavBarVisibilityChange:(BOOL)animated {
  615. [self.navigationController setNavigationBarHidden:[self.navigatorStyle[@"navBarHidden"] boolValue] animated:animated];
  616. }
  617. - (UIStatusBarStyle)preferredStatusBarStyle
  618. {
  619. if (self._statusBarTextColorSchemeLight){
  620. return UIStatusBarStyleLightContent;
  621. } else {
  622. return UIStatusBarStyleDefault;
  623. }
  624. }
  625. - (UIImageView *)findHairlineImageViewUnder:(UIView *)view {
  626. if ([view isKindOfClass:UIImageView.class] && view.bounds.size.height <= 1.0) {
  627. return (UIImageView *)view;
  628. }
  629. for (UIView *subview in view.subviews) {
  630. UIImageView *imageView = [self findHairlineImageViewUnder:subview];
  631. if (imageView) {
  632. return imageView;
  633. }
  634. }
  635. return nil;
  636. }
  637. -(void)addExternalVCIfNecessary:(NSDictionary*)props
  638. {
  639. NSString *externalScreenClass = props[@"externalNativeScreenClass"];
  640. if (externalScreenClass != nil)
  641. {
  642. Class class = NSClassFromString(externalScreenClass);
  643. if (class != NULL)
  644. {
  645. id obj = [[class alloc] init];
  646. if (obj != nil && [obj isKindOfClass:[UIViewController class]] && [obj conformsToProtocol:@protocol(RCCExternalViewControllerProtocol)])
  647. {
  648. ((id <RCCExternalViewControllerProtocol>)obj).controllerDelegate = self;
  649. [obj setProps:props[@"externalNativeScreenProps"]];
  650. UIViewController *viewController = (UIViewController*)obj;
  651. [self addChildViewController:viewController];
  652. viewController.view.frame = self.view.bounds;
  653. [self.view addSubview:viewController.view];
  654. [viewController didMoveToParentViewController:self];
  655. }
  656. else
  657. {
  658. NSLog(@"addExternalVCIfNecessary: could not create instance. Make sure that your class is a UIViewController whihc confirms to RCCExternalViewControllerProtocol");
  659. }
  660. }
  661. else
  662. {
  663. NSLog(@"addExternalVCIfNecessary: could not create class from string. Check that the proper class name wass passed in ExternalNativeScreenClass");
  664. }
  665. }
  666. }
  667. #pragma mark - Preview Actions
  668. - (void)onActionPress:(NSString *)id {
  669. if ([self.view isKindOfClass:[RCTRootView class]]) {
  670. RCTRootView *rootView = (RCTRootView *)self.view;
  671. if (rootView.appProperties && rootView.appProperties[@"navigatorEventID"]) {
  672. [[[RCCManager sharedInstance] getBridge].eventDispatcher
  673. sendAppEventWithName:rootView.appProperties[@"navigatorEventID"]
  674. body:@{
  675. @"type": @"PreviewActionPress",
  676. @"id": id
  677. }];
  678. }
  679. }
  680. }
  681. - (UIPreviewAction *) convertAction:(NSDictionary *)action {
  682. NSString *actionId = action[@"id"];
  683. NSString *actionTitle = action[@"title"];
  684. UIPreviewActionStyle actionStyle = UIPreviewActionStyleDefault;
  685. if ([action[@"style"] isEqualToString:@"selected"]) {
  686. actionStyle = UIPreviewActionStyleSelected;
  687. }
  688. if ([action[@"style"] isEqualToString:@"destructive"]) {
  689. actionStyle = UIPreviewActionStyleDestructive;
  690. }
  691. return [UIPreviewAction actionWithTitle:actionTitle style:actionStyle handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
  692. [self onActionPress:actionId];
  693. }];
  694. }
  695. #pragma mark - NewRelic
  696. - (NSString*) customNewRelicInteractionName
  697. {
  698. NSString *interactionName = nil;
  699. if (self.view != nil && [self.view isKindOfClass:[RCTRootView class]])
  700. {
  701. NSString *moduleName = ((RCTRootView*)self.view).moduleName;
  702. if(moduleName != nil)
  703. {
  704. interactionName = [NSString stringWithFormat:@"RCCViewController: %@", moduleName];
  705. }
  706. }
  707. if (interactionName == nil)
  708. {
  709. interactionName = [NSString stringWithFormat:@"RCCViewController with title: %@", self.title];
  710. }
  711. return interactionName;
  712. }
  713. #pragma mark - UIGestureRecognizerDelegate
  714. -(BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
  715. NSNumber *disabledBackGesture = self.navigatorStyle[@"disabledBackGesture"];
  716. BOOL disabledBackGestureBool = disabledBackGesture ? [disabledBackGesture boolValue] : NO;
  717. return !disabledBackGestureBool;
  718. }
  719. -(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
  720. NSNumber *disabledSimultaneousGesture = self.navigatorStyle[@"disabledSimultaneousGesture"];
  721. BOOL disabledSimultaneousGestureBool = disabledSimultaneousGesture ? [disabledSimultaneousGesture boolValue] : YES; // make default value of disabledSimultaneousGesture is true
  722. return !disabledSimultaneousGestureBool;
  723. }
  724. #pragma mark - UIViewControllerPreviewingDelegate
  725. - (UIViewController *)previewingContext:(id<UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location {
  726. return self.previewController;
  727. }
  728. - (void)previewingContext:(id<UIViewControllerPreviewing>)previewingContext commitViewController:(UIViewController *)viewControllerToCommit {
  729. if (self.previewController.previewCommit == YES) {
  730. [self.previewController sendGlobalScreenEvent:@"willCommitPreview" endTimestampString:[self.previewController getTimestampString] shouldReset:YES];
  731. [self.previewController sendScreenChangedEvent:@"willCommitPreview"];
  732. [self.navigationController pushViewController:self.previewController animated:false];
  733. }
  734. }
  735. - (NSArray<id<UIPreviewActionItem>> *)previewActionItems {
  736. NSMutableArray *actions = [[NSMutableArray alloc] init];
  737. for (NSDictionary *previewAction in self.previewActions) {
  738. UIPreviewAction *action = [self convertAction:previewAction];
  739. NSDictionary *actionActions = previewAction[@"actions"];
  740. if (actionActions.count > 0) {
  741. NSMutableArray *group = [[NSMutableArray alloc] init];
  742. for (NSDictionary *previewGroupAction in actionActions) {
  743. [group addObject:[self convertAction:previewGroupAction]];
  744. }
  745. UIPreviewActionGroup *actionGroup = [UIPreviewActionGroup actionGroupWithTitle:action.title style:UIPreviewActionStyleDefault actions:group];
  746. [actions addObject:actionGroup];
  747. } else {
  748. [actions addObject:action];
  749. }
  750. }
  751. return actions;
  752. }
  753. @end