react-native-navigation的迁移库

RCCViewController.m 30KB

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