react-native-navigation的迁移库

RCCViewController.m 30KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765
  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>
  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. }
  81. // set background image at root level
  82. NSString *rootBackgroundImageName = props[@"style"][@"rootBackgroundImageName"];
  83. if (rootBackgroundImageName) {
  84. UIImage *image = [UIImage imageNamed: rootBackgroundImageName];
  85. UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
  86. [controller.view insertSubview:imageView atIndex:0];
  87. }
  88. return controller;
  89. }
  90. -(NSDictionary*)addCommandTypeAndTimestampIfExists:(NSDictionary*)globalProps passProps:(NSDictionary*)passProps {
  91. NSMutableDictionary *modifiedPassProps = [NSMutableDictionary dictionaryWithDictionary:passProps];
  92. NSString *commandType = globalProps[GLOBAL_SCREEN_ACTION_COMMAND_TYPE];
  93. if (commandType) {
  94. modifiedPassProps[GLOBAL_SCREEN_ACTION_COMMAND_TYPE] = commandType;
  95. }
  96. NSString *timestamp = globalProps[GLOBAL_SCREEN_ACTION_TIMESTAMP];
  97. if (timestamp) {
  98. modifiedPassProps[GLOBAL_SCREEN_ACTION_TIMESTAMP] = timestamp;
  99. }
  100. return modifiedPassProps;
  101. }
  102. - (instancetype)initWithProps:(NSDictionary *)props children:(NSArray *)children globalProps:(NSDictionary *)globalProps bridge:(RCTBridge *)bridge
  103. {
  104. NSString *component = props[@"component"];
  105. if (!component) return nil;
  106. NSDictionary *passProps = props[@"passProps"];
  107. NSDictionary *navigatorStyle = props[@"style"];
  108. NSMutableDictionary *mergedProps = [NSMutableDictionary dictionaryWithDictionary:globalProps];
  109. [mergedProps addEntriesFromDictionary:passProps];
  110. RCTRootView *reactView = [[RCTRootView alloc] initWithBridge:bridge moduleName:component initialProperties:mergedProps];
  111. if (!reactView) return nil;
  112. self = [super init];
  113. if (!self) return nil;
  114. [self commonInit:reactView navigatorStyle:navigatorStyle props:props];
  115. self.navigationController.interactivePopGestureRecognizer.delegate = self;
  116. return self;
  117. }
  118. - (instancetype)initWithComponent:(NSString *)component passProps:(NSDictionary *)passProps navigatorStyle:(NSDictionary*)navigatorStyle globalProps:(NSDictionary *)globalProps bridge:(RCTBridge *)bridge
  119. {
  120. NSMutableDictionary *mergedProps = [NSMutableDictionary dictionaryWithDictionary:globalProps];
  121. [mergedProps addEntriesFromDictionary:passProps];
  122. RCTRootView *reactView = [[RCTRootView alloc] initWithBridge:bridge moduleName:component initialProperties:mergedProps];
  123. if (!reactView) return nil;
  124. self = [super init];
  125. if (!self) return nil;
  126. NSDictionary *modifiedPassProps = [self addCommandTypeAndTimestampIfExists:globalProps passProps:passProps];
  127. [self commonInit:reactView navigatorStyle:navigatorStyle props:modifiedPassProps];
  128. return self;
  129. }
  130. - (void)commonInit:(RCTRootView*)reactView navigatorStyle:(NSDictionary*)navigatorStyle props:(NSDictionary*)props
  131. {
  132. self.view = reactView;
  133. self.edgesForExtendedLayout = UIRectEdgeNone; // default
  134. self.automaticallyAdjustsScrollViewInsets = NO; // default
  135. self.navigatorStyle = [NSMutableDictionary dictionaryWithDictionary:navigatorStyle];
  136. [self setStyleOnInit];
  137. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onRNReload) name:RCTJavaScriptWillStartLoadingNotification object:nil];
  138. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onCancelReactTouches) name:RCCViewControllerCancelReactTouchesNotification object:nil];
  139. self.commandType = props[GLOBAL_SCREEN_ACTION_COMMAND_TYPE];
  140. self.timestamp = props[GLOBAL_SCREEN_ACTION_TIMESTAMP];
  141. // In order to support 3rd party native ViewControllers, we support passing a class name as a prop mamed `ExternalNativeScreenClass`
  142. // In this case, we create an instance and add it as a child ViewController which preserves the VC lifecycle.
  143. // In case some props are necessary in the native ViewController, the ExternalNativeScreenProps can be used to pass them
  144. [self addExternalVCIfNecessary:props];
  145. }
  146. - (void)dealloc
  147. {
  148. [[NSNotificationCenter defaultCenter] removeObserver:self];
  149. self.view = nil;
  150. }
  151. -(void)onRNReload
  152. {
  153. [[NSNotificationCenter defaultCenter] removeObserver:self];
  154. self.view = nil;
  155. }
  156. -(void)onCancelReactTouches
  157. {
  158. if ([self.view isKindOfClass:[RCTRootView class]]){
  159. [(RCTRootView*)self.view cancelTouches];
  160. }
  161. }
  162. - (void)sendScreenChangedEvent:(NSString *)eventName
  163. {
  164. if ([self.view isKindOfClass:[RCTRootView class]]){
  165. RCTRootView *rootView = (RCTRootView *)self.view;
  166. if (rootView.appProperties && rootView.appProperties[@"navigatorEventID"]) {
  167. [[[RCCManager sharedInstance] getBridge].eventDispatcher sendAppEventWithName:rootView.appProperties[@"navigatorEventID"] body:@
  168. {
  169. @"type": @"ScreenChangedEvent",
  170. @"id": eventName
  171. }];
  172. }
  173. }
  174. }
  175. - (void)sendGlobalScreenEvent:(NSString *)eventName endTimestampString:(NSString *)endTimestampStr shouldReset:(BOOL)shouldReset {
  176. if (!self.commandType) return;
  177. if ([self.view isKindOfClass:[RCTRootView class]]){
  178. NSString *screenName = [((RCTRootView*)self.view) moduleName];
  179. [[[RCCManager sharedInstance] getBridge].eventDispatcher sendAppEventWithName:eventName body:@
  180. {
  181. @"commandType": self.commandType ? self.commandType : @"",
  182. @"screen": screenName ? screenName : @"",
  183. @"startTime": self.timestamp ? self.timestamp : @"",
  184. @"endTime": endTimestampStr ? endTimestampStr : @""
  185. }];
  186. if (shouldReset) {
  187. self.commandType = nil;
  188. self.timestamp = nil;
  189. }
  190. }
  191. }
  192. -(BOOL)isDisappearTriggeredFromPop:(NSString *)eventName {
  193. NSArray *navigationViewControllers = self.navigationController.viewControllers;
  194. if (navigationViewControllers.lastObject == self || [navigationViewControllers indexOfObject:self] == NSNotFound) {
  195. return YES;
  196. }
  197. return NO;
  198. }
  199. - (NSString *)getTimestampString {
  200. long long milliseconds = (long long)([[NSDate date] timeIntervalSince1970] * 1000.0);
  201. return [NSString stringWithFormat:@"%lld", milliseconds];
  202. }
  203. - (void)viewDidAppear:(BOOL)animated
  204. {
  205. [super viewDidAppear:animated];
  206. [self sendGlobalScreenEvent:@"didAppear" endTimestampString:[self getTimestampString] shouldReset:YES];
  207. [self sendScreenChangedEvent:@"didAppear"];
  208. }
  209. - (void)viewWillAppear:(BOOL)animated
  210. {
  211. [super viewWillAppear:animated];
  212. [self sendGlobalScreenEvent:@"willAppear" endTimestampString:[self getTimestampString] shouldReset:NO];
  213. [self sendScreenChangedEvent:@"willAppear"];
  214. [self setStyleOnAppear];
  215. }
  216. - (void)viewDidDisappear:(BOOL)animated
  217. {
  218. [super viewDidDisappear:animated];
  219. [self sendGlobalScreenEvent:@"didDisappear" endTimestampString:[self getTimestampString] shouldReset:YES];
  220. [self sendScreenChangedEvent:@"didDisappear"];
  221. }
  222. - (void)viewWillDisappear:(BOOL)animated
  223. {
  224. [super viewWillDisappear:animated];
  225. [self sendGlobalScreenEvent:@"willDisappear" endTimestampString:[self getTimestampString] shouldReset:NO];
  226. [self sendScreenChangedEvent:@"willDisappear"];
  227. [self setStyleOnDisappear];
  228. }
  229. // most styles should be set here since when we pop a view controller that changed them
  230. // we want to reset the style to what we expect (so we need to reset on every willAppear)
  231. - (void)setStyleOnAppear
  232. {
  233. [self setStyleOnAppearForViewController:self appeared:false];
  234. }
  235. - (void)updateStyle
  236. {
  237. [self setStyleOnAppearForViewController:self appeared:true];
  238. }
  239. -(void)setStyleOnAppearForViewController:(UIViewController*)viewController appeared:(BOOL)appeared
  240. {
  241. NSString *screenBackgroundColor = self.navigatorStyle[@"screenBackgroundColor"];
  242. if (screenBackgroundColor) {
  243. UIColor *color = screenBackgroundColor != (id)[NSNull null] ? [RCTConvert UIColor:screenBackgroundColor] : nil;
  244. viewController.view.backgroundColor = color;
  245. }
  246. NSString *screenBackgroundImageName = self.navigatorStyle[@"screenBackgroundImageName"];
  247. if (screenBackgroundImageName) {
  248. UIImage *image = [UIImage imageNamed: screenBackgroundImageName];
  249. viewController.view.layer.contents = (__bridge id _Nullable)(image.CGImage);
  250. }
  251. NSString *navBarBackgroundColor = self.navigatorStyle[@"navBarBackgroundColor"];
  252. if (navBarBackgroundColor) {
  253. UIColor *color = navBarBackgroundColor != (id)[NSNull null] ? [RCTConvert UIColor:navBarBackgroundColor] : nil;
  254. viewController.navigationController.navigationBar.barTintColor = color;
  255. } else {
  256. viewController.navigationController.navigationBar.barTintColor = nil;
  257. }
  258. NSMutableDictionary *titleTextAttributes = [RCTHelpers textAttributesFromDictionary:self.navigatorStyle withPrefix:@"navBarText" baseFont:[UIFont boldSystemFontOfSize:17]];
  259. [self.navigationController.navigationBar setTitleTextAttributes:titleTextAttributes];
  260. if (self.navigationItem.titleView && [self.navigationItem.titleView isKindOfClass:[RCCTitleView class]]) {
  261. RCCTitleView *titleView = (RCCTitleView *)self.navigationItem.titleView;
  262. RCCTitleViewHelper *helper = [[RCCTitleViewHelper alloc] init:viewController navigationController:viewController.navigationController title:titleView.titleLabel.text subtitle:titleView.subtitleLabel.text titleImageData:nil isSetSubtitle:NO];
  263. [helper setup:self.navigatorStyle];
  264. }
  265. NSMutableDictionary *navButtonTextAttributes = [RCTHelpers textAttributesFromDictionary:self.navigatorStyle withPrefix:@"navBarButton"];
  266. NSMutableDictionary *leftNavButtonTextAttributes = [RCTHelpers textAttributesFromDictionary:self.navigatorStyle withPrefix:@"navBarLeftButton"];
  267. NSMutableDictionary *rightNavButtonTextAttributes = [RCTHelpers textAttributesFromDictionary:self.navigatorStyle withPrefix:@"navBarRightButton"];
  268. if (
  269. navButtonTextAttributes.allKeys.count > 0 ||
  270. leftNavButtonTextAttributes.allKeys.count > 0 ||
  271. rightNavButtonTextAttributes.allKeys.count > 0
  272. ) {
  273. for (UIBarButtonItem *item in viewController.navigationItem.leftBarButtonItems) {
  274. [item setTitleTextAttributes:navButtonTextAttributes forState:UIControlStateNormal];
  275. if (leftNavButtonTextAttributes.allKeys.count > 0) {
  276. [item setTitleTextAttributes:leftNavButtonTextAttributes forState:UIControlStateNormal];
  277. }
  278. }
  279. for (UIBarButtonItem *item in viewController.navigationItem.rightBarButtonItems) {
  280. [item setTitleTextAttributes:navButtonTextAttributes forState:UIControlStateNormal];
  281. if (rightNavButtonTextAttributes.allKeys.count > 0) {
  282. [item setTitleTextAttributes:rightNavButtonTextAttributes forState:UIControlStateNormal];
  283. }
  284. }
  285. // At the moment, this seems to be the only thing that gets the back button correctly
  286. [navButtonTextAttributes removeObjectForKey:NSForegroundColorAttributeName];
  287. [[UIBarButtonItem appearance] setTitleTextAttributes:navButtonTextAttributes forState:UIControlStateNormal];
  288. }
  289. NSString *navBarButtonColor = self.navigatorStyle[@"navBarButtonColor"];
  290. if (navBarButtonColor) {
  291. UIColor *color = navBarButtonColor != (id)[NSNull null] ? [RCTConvert UIColor:navBarButtonColor] : nil;
  292. viewController.navigationController.navigationBar.tintColor = color;
  293. } else
  294. {
  295. viewController.navigationController.navigationBar.tintColor = nil;
  296. }
  297. BOOL viewControllerBasedStatusBar = false;
  298. NSObject *viewControllerBasedStatusBarAppearance = [[NSBundle mainBundle] infoDictionary][@"UIViewControllerBasedStatusBarAppearance"];
  299. if (viewControllerBasedStatusBarAppearance && [viewControllerBasedStatusBarAppearance isKindOfClass:[NSNumber class]]) {
  300. viewControllerBasedStatusBar = [(NSNumber *)viewControllerBasedStatusBarAppearance boolValue];
  301. }
  302. NSString *statusBarTextColorSchemeSingleScreen = self.navigatorStyle[@"statusBarTextColorSchemeSingleScreen"];
  303. NSString *statusBarTextColorScheme = self.navigatorStyle[@"statusBarTextColorScheme"];
  304. NSString *finalColorScheme = statusBarTextColorSchemeSingleScreen ? : statusBarTextColorScheme;
  305. if (finalColorScheme && [finalColorScheme isEqualToString:@"light"]) {
  306. if (!statusBarTextColorSchemeSingleScreen) {
  307. viewController.navigationController.navigationBar.barStyle = UIBarStyleBlack;
  308. }
  309. self._statusBarTextColorSchemeLight = true;
  310. if (!viewControllerBasedStatusBarAppearance) {
  311. [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
  312. }
  313. [viewController setNeedsStatusBarAppearanceUpdate];
  314. } else {
  315. if (!statusBarTextColorSchemeSingleScreen) {
  316. viewController.navigationController.navigationBar.barStyle = UIBarStyleDefault;
  317. }
  318. self._statusBarTextColorSchemeLight = false;
  319. if (!viewControllerBasedStatusBarAppearance) {
  320. [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
  321. }
  322. [viewController setNeedsStatusBarAppearanceUpdate];
  323. }
  324. NSNumber *navBarHidden = self.navigatorStyle[@"navBarHidden"];
  325. BOOL navBarHiddenBool = navBarHidden ? [navBarHidden boolValue] : NO;
  326. if (viewController.navigationController.navigationBarHidden != navBarHiddenBool) {
  327. [viewController.navigationController setNavigationBarHidden:navBarHiddenBool animated:YES];
  328. }
  329. NSNumber *navBarHideOnScroll = self.navigatorStyle[@"navBarHideOnScroll"];
  330. BOOL navBarHideOnScrollBool = navBarHideOnScroll ? [navBarHideOnScroll boolValue] : NO;
  331. if (navBarHideOnScrollBool) {
  332. viewController.navigationController.hidesBarsOnSwipe = YES;
  333. } else {
  334. viewController.navigationController.hidesBarsOnSwipe = NO;
  335. }
  336. NSNumber *statusBarBlur = self.navigatorStyle[@"statusBarBlur"];
  337. BOOL statusBarBlurBool = statusBarBlur ? [statusBarBlur boolValue] : NO;
  338. if (statusBarBlurBool && ![viewController.view viewWithTag:BLUR_STATUS_TAG]) {
  339. UIVisualEffectView *blur = [[UIVisualEffectView alloc] initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleLight]];
  340. blur.frame = [[UIApplication sharedApplication] statusBarFrame];
  341. blur.tag = BLUR_STATUS_TAG;
  342. [viewController.view insertSubview:blur atIndex:0];
  343. }
  344. NSNumber *navBarBlur = self.navigatorStyle[@"navBarBlur"];
  345. BOOL navBarBlurBool = navBarBlur ? [navBarBlur boolValue] : NO;
  346. if (navBarBlurBool) {
  347. if (![viewController.navigationController.navigationBar viewWithTag:BLUR_NAVBAR_TAG]) {
  348. [self storeOriginalNavBarImages];
  349. [viewController.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
  350. viewController.navigationController.navigationBar.shadowImage = [UIImage new];
  351. UIVisualEffectView *blur = [[UIVisualEffectView alloc] initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleLight]];
  352. CGRect statusBarFrame = [[UIApplication sharedApplication] statusBarFrame];
  353. blur.frame = CGRectMake(0, -1 * statusBarFrame.size.height, viewController.navigationController.navigationBar.frame.size.width, viewController.navigationController.navigationBar.frame.size.height + statusBarFrame.size.height);
  354. blur.userInteractionEnabled = NO;
  355. blur.tag = BLUR_NAVBAR_TAG;
  356. [viewController.navigationController.navigationBar insertSubview:blur atIndex:0];
  357. [viewController.navigationController.navigationBar sendSubviewToBack:blur];
  358. }
  359. } else {
  360. UIView *blur = [viewController.navigationController.navigationBar viewWithTag:BLUR_NAVBAR_TAG];
  361. if (blur) {
  362. [blur removeFromSuperview];
  363. [viewController.navigationController.navigationBar setBackgroundImage:self.originalNavBarImages[@"bgImage"] forBarMetrics:UIBarMetricsDefault];
  364. viewController.navigationController.navigationBar.shadowImage = self.originalNavBarImages[@"shadowImage"];
  365. self.originalNavBarImages = nil;
  366. }
  367. }
  368. NSNumber *navBarTransparent = self.navigatorStyle[@"navBarTransparent"];
  369. BOOL navBarTransparentBool = navBarTransparent ? [navBarTransparent boolValue] : NO;
  370. void (^action)() = ^ {
  371. if (navBarTransparentBool)
  372. {
  373. if (![viewController.navigationController.navigationBar viewWithTag:TRANSPARENT_NAVBAR_TAG])
  374. {
  375. [self storeOriginalNavBarImages];
  376. [viewController.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
  377. viewController.navigationController.navigationBar.shadowImage = [UIImage new];
  378. UIView *transparentView = [[UIView alloc] initWithFrame:CGRectZero];
  379. transparentView.tag = TRANSPARENT_NAVBAR_TAG;
  380. [viewController.navigationController.navigationBar insertSubview:transparentView atIndex:0];
  381. }
  382. }
  383. else
  384. {
  385. UIView *transparentView = [viewController.navigationController.navigationBar viewWithTag:TRANSPARENT_NAVBAR_TAG];
  386. if (transparentView)
  387. {
  388. [transparentView removeFromSuperview];
  389. [viewController.navigationController.navigationBar setBackgroundImage:self.originalNavBarImages[@"bgImage"] forBarMetrics:UIBarMetricsDefault];
  390. viewController.navigationController.navigationBar.shadowImage = self.originalNavBarImages[@"shadowImage"];
  391. self.originalNavBarImages = nil;
  392. }
  393. }
  394. };
  395. if (!self.transitionCoordinator || self.transitionCoordinator.initiallyInteractive || !navBarTransparentBool || appeared) {
  396. action();
  397. } else {
  398. UIView* backgroundView = [self.navigationController.navigationBar valueForKey:@"backgroundView"];
  399. CGFloat originalAlpha = backgroundView.alpha;
  400. backgroundView.alpha = navBarTransparentBool ? 0.0 : 1.0;
  401. [self.transitionCoordinator animateAlongsideTransition:nil completion:^(id<UIViewControllerTransitionCoordinatorContext> _Nonnull context) {
  402. action();
  403. backgroundView.alpha = originalAlpha;
  404. }];
  405. }
  406. NSNumber *autoAdjustsScrollViewInsets = self.navigatorStyle[@"autoAdjustScrollViewInsets"];
  407. viewController.automaticallyAdjustsScrollViewInsets = autoAdjustsScrollViewInsets ? [autoAdjustsScrollViewInsets boolValue] : false;
  408. NSNumber *navBarTranslucent = self.navigatorStyle[@"navBarTranslucent"];
  409. BOOL navBarTranslucentBool = navBarTranslucent ? [navBarTranslucent boolValue] : NO;
  410. if (navBarTranslucentBool || navBarBlurBool) {
  411. viewController.navigationController.navigationBar.translucent = YES;
  412. } else {
  413. viewController.navigationController.navigationBar.translucent = NO;
  414. }
  415. NSNumber *extendedLayoutIncludesOpaqueBars = self.navigatorStyle[@"extendedLayoutIncludesOpaqueBars"];
  416. BOOL extendedLayoutIncludesOpaqueBarsBool = extendedLayoutIncludesOpaqueBars ? [extendedLayoutIncludesOpaqueBars boolValue] : NO;
  417. viewController.extendedLayoutIncludesOpaqueBars = extendedLayoutIncludesOpaqueBarsBool;
  418. NSNumber *drawUnderNavBar = self.navigatorStyle[@"drawUnderNavBar"];
  419. BOOL drawUnderNavBarBool = drawUnderNavBar ? [drawUnderNavBar boolValue] : NO;
  420. if (drawUnderNavBarBool) {
  421. viewController.edgesForExtendedLayout |= UIRectEdgeTop;
  422. }
  423. else {
  424. viewController.edgesForExtendedLayout &= ~UIRectEdgeTop;
  425. }
  426. NSNumber *drawUnderTabBar = self.navigatorStyle[@"drawUnderTabBar"];
  427. BOOL drawUnderTabBarBool = drawUnderTabBar ? [drawUnderTabBar boolValue] : NO;
  428. if (drawUnderTabBarBool) {
  429. viewController.edgesForExtendedLayout |= UIRectEdgeBottom;
  430. } else {
  431. viewController.edgesForExtendedLayout &= ~UIRectEdgeBottom;
  432. }
  433. NSNumber *removeNavBarBorder = self.navigatorStyle[@"navBarNoBorder"];
  434. BOOL removeNavBarBorderBool = removeNavBarBorder ? [removeNavBarBorder boolValue] : NO;
  435. if (removeNavBarBorderBool) {
  436. self.navBarHairlineImageView.hidden = YES;
  437. } else {
  438. self.navBarHairlineImageView.hidden = NO;
  439. }
  440. //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
  441. //overriding the delegate of the gesture prevents this from happening while keeping the gesture intact (another option was to disable it completely by demand)
  442. self.originalInteractivePopGestureDelegate = nil;
  443. if(self.navigationController.viewControllers.count > 1){
  444. if (self.navigationController != nil && self.navigationController.interactivePopGestureRecognizer != nil)
  445. {
  446. id <UIGestureRecognizerDelegate> interactivePopGestureRecognizer = self.navigationController.interactivePopGestureRecognizer.delegate;
  447. if (interactivePopGestureRecognizer != nil)
  448. {
  449. self.originalInteractivePopGestureDelegate = interactivePopGestureRecognizer;
  450. self.navigationController.interactivePopGestureRecognizer.delegate = self;
  451. }
  452. }
  453. }
  454. NSString *navBarCustomView = self.navigatorStyle[@"navBarCustomView"];
  455. if (navBarCustomView && ![self.navigationItem.titleView isKindOfClass:[RCCCustomTitleView class]]) {
  456. if ([self.view isKindOfClass:[RCTRootView class]]) {
  457. RCTBridge *bridge = ((RCTRootView*)self.view).bridge;
  458. NSDictionary *initialProps = self.navigatorStyle[@"navBarCustomViewInitialProps"];
  459. RCTRootView *reactView = [[RCTRootView alloc] initWithBridge:bridge moduleName:navBarCustomView initialProperties:initialProps];
  460. RCCCustomTitleView *titleView = [[RCCCustomTitleView alloc] initWithFrame:self.navigationController.navigationBar.bounds subView:reactView alignment:self.navigatorStyle[@"navBarComponentAlignment"]];
  461. titleView.backgroundColor = [UIColor clearColor];
  462. reactView.backgroundColor = [UIColor clearColor];
  463. self.navigationItem.titleView = titleView;
  464. self.navigationItem.titleView.backgroundColor = [UIColor clearColor];
  465. self.navigationItem.titleView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
  466. self.navigationItem.titleView.clipsToBounds = YES;
  467. }
  468. }
  469. }
  470. -(void)storeOriginalNavBarImages {
  471. NSMutableDictionary *originalNavBarImages = [@{} mutableCopy];
  472. UIImage *bgImage = [self.navigationController.navigationBar backgroundImageForBarMetrics:UIBarMetricsDefault];
  473. if (bgImage != nil) {
  474. originalNavBarImages[@"bgImage"] = bgImage;
  475. }
  476. UIImage *shadowImage = self.navigationController.navigationBar.shadowImage;
  477. if (shadowImage != nil) {
  478. originalNavBarImages[@"shadowImage"] = shadowImage;
  479. }
  480. self.originalNavBarImages = originalNavBarImages;
  481. }
  482. -(void)setStyleOnDisappear {
  483. self.navBarHairlineImageView.hidden = NO;
  484. if (self.navigationController != nil && self.navigationController.interactivePopGestureRecognizer != nil && self.originalInteractivePopGestureDelegate != nil)
  485. {
  486. self.navigationController.interactivePopGestureRecognizer.delegate = self.originalInteractivePopGestureDelegate;
  487. self.originalInteractivePopGestureDelegate = nil;
  488. }
  489. }
  490. // only styles that can't be set on willAppear should be set here
  491. - (void)setStyleOnInit
  492. {
  493. NSNumber *tabBarHidden = self.navigatorStyle[@"tabBarHidden"];
  494. BOOL tabBarHiddenBool = tabBarHidden ? [tabBarHidden boolValue] : NO;
  495. if (tabBarHiddenBool) {
  496. self._hidesBottomBarWhenPushed = YES;
  497. } else {
  498. self._hidesBottomBarWhenPushed = NO;
  499. }
  500. NSNumber *statusBarHideWithNavBar = self.navigatorStyle[@"statusBarHideWithNavBar"];
  501. BOOL statusBarHideWithNavBarBool = statusBarHideWithNavBar ? [statusBarHideWithNavBar boolValue] : NO;
  502. if (statusBarHideWithNavBarBool) {
  503. self._statusBarHideWithNavBar = YES;
  504. } else {
  505. self._statusBarHideWithNavBar = NO;
  506. }
  507. NSNumber *statusBarHidden = self.navigatorStyle[@"statusBarHidden"];
  508. BOOL statusBarHiddenBool = statusBarHidden ? [statusBarHidden boolValue] : NO;
  509. if (statusBarHiddenBool) {
  510. self._statusBarHidden = YES;
  511. } else {
  512. self._statusBarHidden = NO;
  513. }
  514. }
  515. - (BOOL)hidesBottomBarWhenPushed
  516. {
  517. if (!self._hidesBottomBarWhenPushed) return NO;
  518. return (self.navigationController.topViewController == self);
  519. }
  520. - (BOOL)prefersStatusBarHidden
  521. {
  522. if (self._statusBarHidden) {
  523. return YES;
  524. }
  525. if (self._statusBarHideWithNavBar) {
  526. return self.navigationController.isNavigationBarHidden;
  527. } else {
  528. return NO;
  529. }
  530. }
  531. - (void)setNavBarVisibilityChange:(BOOL)animated {
  532. [self.navigationController setNavigationBarHidden:[self.navigatorStyle[@"navBarHidden"] boolValue] animated:animated];
  533. }
  534. - (UIStatusBarStyle)preferredStatusBarStyle
  535. {
  536. if (self._statusBarTextColorSchemeLight){
  537. return UIStatusBarStyleLightContent;
  538. } else {
  539. return UIStatusBarStyleDefault;
  540. }
  541. }
  542. - (UIImageView *)findHairlineImageViewUnder:(UIView *)view {
  543. if ([view isKindOfClass:UIImageView.class] && view.bounds.size.height <= 1.0) {
  544. return (UIImageView *)view;
  545. }
  546. for (UIView *subview in view.subviews) {
  547. UIImageView *imageView = [self findHairlineImageViewUnder:subview];
  548. if (imageView) {
  549. return imageView;
  550. }
  551. }
  552. return nil;
  553. }
  554. -(void)addExternalVCIfNecessary:(NSDictionary*)props
  555. {
  556. NSString *externalScreenClass = props[@"externalNativeScreenClass"];
  557. if (externalScreenClass != nil)
  558. {
  559. Class class = NSClassFromString(externalScreenClass);
  560. if (class != NULL)
  561. {
  562. id obj = [[class alloc] init];
  563. if (obj != nil && [obj isKindOfClass:[UIViewController class]] && [obj conformsToProtocol:@protocol(RCCExternalViewControllerProtocol)])
  564. {
  565. ((id <RCCExternalViewControllerProtocol>)obj).controllerDelegate = self;
  566. [obj setProps:props[@"externalNativeScreenProps"]];
  567. UIViewController *viewController = (UIViewController*)obj;
  568. [self addChildViewController:viewController];
  569. viewController.view.frame = self.view.bounds;
  570. [self.view addSubview:viewController.view];
  571. [viewController didMoveToParentViewController:self];
  572. }
  573. else
  574. {
  575. NSLog(@"addExternalVCIfNecessary: could not create instance. Make sure that your class is a UIViewController whihc confirms to RCCExternalViewControllerProtocol");
  576. }
  577. }
  578. else
  579. {
  580. NSLog(@"addExternalVCIfNecessary: could not create class from string. Check that the proper class name wass passed in ExternalNativeScreenClass");
  581. }
  582. }
  583. }
  584. #pragma mark - NewRelic
  585. - (NSString*) customNewRelicInteractionName
  586. {
  587. NSString *interactionName = nil;
  588. if (self.view != nil && [self.view isKindOfClass:[RCTRootView class]])
  589. {
  590. NSString *moduleName = ((RCTRootView*)self.view).moduleName;
  591. if(moduleName != nil)
  592. {
  593. interactionName = [NSString stringWithFormat:@"RCCViewController: %@", moduleName];
  594. }
  595. }
  596. if (interactionName == nil)
  597. {
  598. interactionName = [NSString stringWithFormat:@"RCCViewController with title: %@", self.title];
  599. }
  600. return interactionName;
  601. }
  602. #pragma mark - UIGestureRecognizerDelegate
  603. -(BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
  604. NSNumber *disabledBackGesture = self.navigatorStyle[@"disabledBackGesture"];
  605. BOOL disabledBackGestureBool = disabledBackGesture ? [disabledBackGesture boolValue] : NO;
  606. return !disabledBackGestureBool;
  607. }
  608. @end