react-native-navigation的迁移库

RCCViewController.m 26KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642
  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. NSString* const RCCViewControllerCancelReactTouchesNotification = @"RCCViewControllerCancelReactTouchesNotification";
  14. const NSInteger BLUR_STATUS_TAG = 78264801;
  15. const NSInteger BLUR_NAVBAR_TAG = 78264802;
  16. const NSInteger TRANSPARENT_NAVBAR_TAG = 78264803;
  17. @interface RCCViewController() <UIGestureRecognizerDelegate>
  18. @property (nonatomic) BOOL _hidesBottomBarWhenPushed;
  19. @property (nonatomic) BOOL _statusBarHideWithNavBar;
  20. @property (nonatomic) BOOL _statusBarHidden;
  21. @property (nonatomic) BOOL _statusBarTextColorSchemeLight;
  22. @property (nonatomic, strong) NSDictionary *originalNavBarImages;
  23. @property (nonatomic, strong) UIImageView *navBarHairlineImageView;
  24. @property (nonatomic, weak) id <UIGestureRecognizerDelegate> originalInteractivePopGestureDelegate;
  25. @end
  26. @implementation RCCViewController
  27. -(UIImageView *)navBarHairlineImageView {
  28. if (!_navBarHairlineImageView) {
  29. _navBarHairlineImageView = [self findHairlineImageViewUnder:self.navigationController.navigationBar];
  30. }
  31. return _navBarHairlineImageView;
  32. }
  33. + (UIViewController*)controllerWithLayout:(NSDictionary *)layout globalProps:(NSDictionary *)globalProps bridge:(RCTBridge *)bridge
  34. {
  35. UIViewController* controller = nil;
  36. if (!layout) return nil;
  37. // get props
  38. if (!layout[@"props"]) return nil;
  39. if (![layout[@"props"] isKindOfClass:[NSDictionary class]]) return nil;
  40. NSDictionary *props = layout[@"props"];
  41. // get children
  42. if (!layout[@"children"]) return nil;
  43. if (![layout[@"children"] isKindOfClass:[NSArray class]]) return nil;
  44. NSArray *children = layout[@"children"];
  45. // create according to type
  46. NSString *type = layout[@"type"];
  47. if (!type) return nil;
  48. // regular view controller
  49. if ([type isEqualToString:@"ViewControllerIOS"])
  50. {
  51. controller = [[RCCViewController alloc] initWithProps:props children:children globalProps:globalProps bridge:bridge];
  52. }
  53. // navigation controller
  54. if ([type isEqualToString:@"NavigationControllerIOS"])
  55. {
  56. controller = [[RCCNavigationController alloc] initWithProps:props children:children globalProps:globalProps bridge:bridge];
  57. }
  58. // tab bar controller
  59. if ([type isEqualToString:@"TabBarControllerIOS"])
  60. {
  61. controller = [[RCCTabBarController alloc] initWithProps:props children:children globalProps:globalProps bridge:bridge];
  62. }
  63. // side menu controller
  64. if ([type isEqualToString:@"DrawerControllerIOS"])
  65. {
  66. NSString *drawerType = props[@"type"];
  67. if ([drawerType isEqualToString:@"TheSideBar"]) {
  68. controller = [[RCCTheSideBarManagerViewController alloc] initWithProps:props children:children globalProps:globalProps bridge:bridge];
  69. }
  70. else {
  71. controller = [[RCCDrawerController alloc] initWithProps:props children:children globalProps:globalProps bridge:bridge];
  72. }
  73. }
  74. // register the controller if we have an id
  75. NSString *componentId = props[@"id"];
  76. if (controller && componentId)
  77. {
  78. [[RCCManager sharedInstance] registerController:controller componentId:componentId componentType:type];
  79. }
  80. return controller;
  81. }
  82. - (instancetype)initWithProps:(NSDictionary *)props children:(NSArray *)children globalProps:(NSDictionary *)globalProps bridge:(RCTBridge *)bridge
  83. {
  84. NSString *component = props[@"component"];
  85. if (!component) return nil;
  86. NSDictionary *passProps = props[@"passProps"];
  87. NSDictionary *navigatorStyle = props[@"style"];
  88. NSMutableDictionary *mergedProps = [NSMutableDictionary dictionaryWithDictionary:globalProps];
  89. [mergedProps addEntriesFromDictionary:passProps];
  90. RCTRootView *reactView = [[RCTRootView alloc] initWithBridge:bridge moduleName:component initialProperties:mergedProps];
  91. if (!reactView) return nil;
  92. self = [super init];
  93. if (!self) return nil;
  94. [self commonInit:reactView navigatorStyle:navigatorStyle props:props];
  95. return self;
  96. }
  97. - (instancetype)initWithComponent:(NSString *)component passProps:(NSDictionary *)passProps navigatorStyle:(NSDictionary*)navigatorStyle globalProps:(NSDictionary *)globalProps bridge:(RCTBridge *)bridge
  98. {
  99. NSMutableDictionary *mergedProps = [NSMutableDictionary dictionaryWithDictionary:globalProps];
  100. [mergedProps addEntriesFromDictionary:passProps];
  101. RCTRootView *reactView = [[RCTRootView alloc] initWithBridge:bridge moduleName:component initialProperties:mergedProps];
  102. if (!reactView) return nil;
  103. self = [super init];
  104. if (!self) return nil;
  105. [self commonInit:reactView navigatorStyle:navigatorStyle props:passProps];
  106. return self;
  107. }
  108. - (void)commonInit:(RCTRootView*)reactView navigatorStyle:(NSDictionary*)navigatorStyle props:(NSDictionary*)props
  109. {
  110. self.view = reactView;
  111. self.edgesForExtendedLayout = UIRectEdgeNone; // default
  112. self.automaticallyAdjustsScrollViewInsets = NO; // default
  113. self.navigatorStyle = [NSMutableDictionary dictionaryWithDictionary:navigatorStyle];
  114. [self setStyleOnInit];
  115. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onRNReload) name:RCTJavaScriptWillStartLoadingNotification object:nil];
  116. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onCancelReactTouches) name:RCCViewControllerCancelReactTouchesNotification object:nil];
  117. // In order to support 3rd party native ViewControllers, we support passing a class name as a prop mamed `ExternalNativeScreenClass`
  118. // In this case, we create an instance and add it as a child ViewController which preserves the VC lifecycle.
  119. // In case some props are necessary in the native ViewController, the ExternalNativeScreenProps can be used to pass them
  120. [self addExternalVCIfNecessary:props];
  121. }
  122. - (void)dealloc
  123. {
  124. [[NSNotificationCenter defaultCenter] removeObserver:self];
  125. self.view = nil;
  126. }
  127. -(void)onRNReload
  128. {
  129. [[NSNotificationCenter defaultCenter] removeObserver:self];
  130. self.view = nil;
  131. }
  132. -(void)onCancelReactTouches
  133. {
  134. if ([self.view isKindOfClass:[RCTRootView class]]){
  135. [(RCTRootView*)self.view cancelTouches];
  136. }
  137. }
  138. - (void)sendScreenChangedEvent:(NSString *)eventName
  139. {
  140. if ([self.view isKindOfClass:[RCTRootView class]]){
  141. RCTRootView *rootView = (RCTRootView *)self.view;
  142. if (rootView.appProperties && rootView.appProperties[@"navigatorEventID"]) {
  143. [[[RCCManager sharedInstance] getBridge].eventDispatcher sendAppEventWithName:rootView.appProperties[@"navigatorEventID"] body:@
  144. {
  145. @"type": @"ScreenChangedEvent",
  146. @"id": eventName
  147. }];
  148. }
  149. }
  150. }
  151. - (void)viewDidAppear:(BOOL)animated
  152. {
  153. [super viewDidAppear:animated];
  154. [self sendScreenChangedEvent:@"didAppear"];
  155. }
  156. - (void)viewWillAppear:(BOOL)animated
  157. {
  158. [super viewWillAppear:animated];
  159. [self sendScreenChangedEvent:@"willAppear"];
  160. [self setStyleOnAppear];
  161. }
  162. - (void)viewDidDisappear:(BOOL)animated
  163. {
  164. [super viewDidDisappear:animated];
  165. [self sendScreenChangedEvent:@"didDisappear"];
  166. }
  167. - (void)viewWillDisappear:(BOOL)animated
  168. {
  169. [super viewWillDisappear:animated];
  170. [self sendScreenChangedEvent:@"willDisappear"];
  171. [self setStyleOnDisappear];
  172. }
  173. // most styles should be set here since when we pop a view controller that changed them
  174. // we want to reset the style to what we expect (so we need to reset on every willAppear)
  175. - (void)setStyleOnAppear
  176. {
  177. [self setStyleOnAppearForViewController:self appeared:false];
  178. }
  179. - (void)updateStyle
  180. {
  181. [self setStyleOnAppearForViewController:self appeared:true];
  182. }
  183. -(void)setStyleOnAppearForViewController:(UIViewController*)viewController appeared:(BOOL)appeared
  184. {
  185. NSString *screenBackgroundColor = self.navigatorStyle[@"screenBackgroundColor"];
  186. if (screenBackgroundColor) {
  187. UIColor *color = screenBackgroundColor != (id)[NSNull null] ? [RCTConvert UIColor:screenBackgroundColor] : nil;
  188. viewController.view.backgroundColor = color;
  189. }
  190. NSString *screenBackgroundImageName = self.navigatorStyle[@"screenBackgroundImageName"];
  191. if (screenBackgroundImageName) {
  192. UIImage *image = [UIImage imageNamed: screenBackgroundImageName];
  193. [viewController.view setBackgroundColor: [UIColor colorWithPatternImage: image]];
  194. }
  195. NSString *navBarBackgroundColor = self.navigatorStyle[@"navBarBackgroundColor"];
  196. if (navBarBackgroundColor) {
  197. UIColor *color = navBarBackgroundColor != (id)[NSNull null] ? [RCTConvert UIColor:navBarBackgroundColor] : nil;
  198. viewController.navigationController.navigationBar.barTintColor = color;
  199. } else {
  200. viewController.navigationController.navigationBar.barTintColor = nil;
  201. }
  202. NSMutableDictionary *titleTextAttributes = [RCTHelpers textAttributesFromDictionary:self.navigatorStyle withPrefix:@"navBarText" baseFont:[UIFont boldSystemFontOfSize:17]];
  203. [self.navigationController.navigationBar setTitleTextAttributes:titleTextAttributes];
  204. if (self.navigationItem.titleView && [self.navigationItem.titleView isKindOfClass:[RCCTitleView class]]) {
  205. RCCTitleView *titleView = (RCCTitleView *)self.navigationItem.titleView;
  206. RCCTitleViewHelper *helper = [[RCCTitleViewHelper alloc] init:viewController navigationController:viewController.navigationController title:titleView.titleLabel.text subtitle:titleView.subtitleLabel.text titleImageData:nil];
  207. [helper setup:self.navigatorStyle];
  208. }
  209. NSMutableDictionary *navButtonTextAttributes = [RCTHelpers textAttributesFromDictionary:self.navigatorStyle withPrefix:@"navBarButton"];
  210. if (navButtonTextAttributes.allKeys.count > 0) {
  211. for (UIBarButtonItem *item in viewController.navigationItem.leftBarButtonItems) {
  212. [item setTitleTextAttributes:navButtonTextAttributes forState:UIControlStateNormal];
  213. }
  214. for (UIBarButtonItem *item in viewController.navigationItem.rightBarButtonItems) {
  215. [item setTitleTextAttributes:navButtonTextAttributes forState:UIControlStateNormal];
  216. }
  217. // At the moment, this seems to be the only thing that gets the back button correctly
  218. [navButtonTextAttributes removeObjectForKey:NSForegroundColorAttributeName];
  219. [[UIBarButtonItem appearance] setTitleTextAttributes:navButtonTextAttributes forState:UIControlStateNormal];
  220. // [viewController.navigationItem.backBarButtonItem setTitleTextAttributes:navButtonTextAttributes forState:UIControlStateNormal];
  221. }
  222. NSString *navBarButtonColor = self.navigatorStyle[@"navBarButtonColor"];
  223. if (navBarButtonColor) {
  224. UIColor *color = navBarButtonColor != (id)[NSNull null] ? [RCTConvert UIColor:navBarButtonColor] : nil;
  225. viewController.navigationController.navigationBar.tintColor = color;
  226. } else
  227. {
  228. viewController.navigationController.navigationBar.tintColor = nil;
  229. }
  230. BOOL viewControllerBasedStatusBar = false;
  231. NSObject *viewControllerBasedStatusBarAppearance = [[NSBundle mainBundle] infoDictionary][@"UIViewControllerBasedStatusBarAppearance"];
  232. if (viewControllerBasedStatusBarAppearance && [viewControllerBasedStatusBarAppearance isKindOfClass:[NSNumber class]]) {
  233. viewControllerBasedStatusBar = [(NSNumber *)viewControllerBasedStatusBarAppearance boolValue];
  234. }
  235. NSString *statusBarTextColorSchemeSingleScreen = self.navigatorStyle[@"statusBarTextColorSchemeSingleScreen"];
  236. NSString *statusBarTextColorScheme = self.navigatorStyle[@"statusBarTextColorScheme"];
  237. NSString *finalColorScheme = statusBarTextColorSchemeSingleScreen ? : statusBarTextColorScheme;
  238. if (finalColorScheme && [finalColorScheme isEqualToString:@"light"]) {
  239. if (!statusBarTextColorSchemeSingleScreen) {
  240. viewController.navigationController.navigationBar.barStyle = UIBarStyleBlack;
  241. }
  242. self._statusBarTextColorSchemeLight = true;
  243. if (!viewControllerBasedStatusBarAppearance) {
  244. [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
  245. }
  246. [viewController setNeedsStatusBarAppearanceUpdate];
  247. } else {
  248. if (!statusBarTextColorSchemeSingleScreen) {
  249. viewController.navigationController.navigationBar.barStyle = UIBarStyleDefault;
  250. }
  251. self._statusBarTextColorSchemeLight = false;
  252. if (!viewControllerBasedStatusBarAppearance) {
  253. [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
  254. }
  255. [viewController setNeedsStatusBarAppearanceUpdate];
  256. }
  257. NSNumber *navBarHidden = self.navigatorStyle[@"navBarHidden"];
  258. BOOL navBarHiddenBool = navBarHidden ? [navBarHidden boolValue] : NO;
  259. if (viewController.navigationController.navigationBarHidden != navBarHiddenBool) {
  260. [viewController.navigationController setNavigationBarHidden:navBarHiddenBool animated:YES];
  261. }
  262. NSNumber *navBarHideOnScroll = self.navigatorStyle[@"navBarHideOnScroll"];
  263. BOOL navBarHideOnScrollBool = navBarHideOnScroll ? [navBarHideOnScroll boolValue] : NO;
  264. if (navBarHideOnScrollBool) {
  265. viewController.navigationController.hidesBarsOnSwipe = YES;
  266. } else {
  267. viewController.navigationController.hidesBarsOnSwipe = NO;
  268. }
  269. NSNumber *statusBarBlur = self.navigatorStyle[@"statusBarBlur"];
  270. BOOL statusBarBlurBool = statusBarBlur ? [statusBarBlur boolValue] : NO;
  271. if (statusBarBlurBool && ![viewController.view viewWithTag:BLUR_STATUS_TAG]) {
  272. UIVisualEffectView *blur = [[UIVisualEffectView alloc] initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleLight]];
  273. blur.frame = [[UIApplication sharedApplication] statusBarFrame];
  274. blur.tag = BLUR_STATUS_TAG;
  275. [viewController.view insertSubview:blur atIndex:0];
  276. }
  277. NSNumber *navBarBlur = self.navigatorStyle[@"navBarBlur"];
  278. BOOL navBarBlurBool = navBarBlur ? [navBarBlur boolValue] : NO;
  279. if (navBarBlurBool) {
  280. if (![viewController.navigationController.navigationBar viewWithTag:BLUR_NAVBAR_TAG]) {
  281. [self storeOriginalNavBarImages];
  282. [viewController.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
  283. viewController.navigationController.navigationBar.shadowImage = [UIImage new];
  284. UIVisualEffectView *blur = [[UIVisualEffectView alloc] initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleLight]];
  285. CGRect statusBarFrame = [[UIApplication sharedApplication] statusBarFrame];
  286. blur.frame = CGRectMake(0, -1 * statusBarFrame.size.height, viewController.navigationController.navigationBar.frame.size.width, viewController.navigationController.navigationBar.frame.size.height + statusBarFrame.size.height);
  287. blur.userInteractionEnabled = NO;
  288. blur.tag = BLUR_NAVBAR_TAG;
  289. [viewController.navigationController.navigationBar insertSubview:blur atIndex:0];
  290. [viewController.navigationController.navigationBar sendSubviewToBack:blur];
  291. }
  292. } else {
  293. UIView *blur = [viewController.navigationController.navigationBar viewWithTag:BLUR_NAVBAR_TAG];
  294. if (blur) {
  295. [blur removeFromSuperview];
  296. [viewController.navigationController.navigationBar setBackgroundImage:self.originalNavBarImages[@"bgImage"] forBarMetrics:UIBarMetricsDefault];
  297. viewController.navigationController.navigationBar.shadowImage = self.originalNavBarImages[@"shadowImage"];
  298. self.originalNavBarImages = nil;
  299. }
  300. }
  301. NSNumber *navBarTransparent = self.navigatorStyle[@"navBarTransparent"];
  302. BOOL navBarTransparentBool = navBarTransparent ? [navBarTransparent boolValue] : NO;
  303. void (^action)() = ^ {
  304. if (navBarTransparentBool)
  305. {
  306. if (![viewController.navigationController.navigationBar viewWithTag:TRANSPARENT_NAVBAR_TAG])
  307. {
  308. [self storeOriginalNavBarImages];
  309. [viewController.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
  310. viewController.navigationController.navigationBar.shadowImage = [UIImage new];
  311. UIView *transparentView = [[UIView alloc] initWithFrame:CGRectZero];
  312. transparentView.tag = TRANSPARENT_NAVBAR_TAG;
  313. [viewController.navigationController.navigationBar insertSubview:transparentView atIndex:0];
  314. }
  315. }
  316. else
  317. {
  318. UIView *transparentView = [viewController.navigationController.navigationBar viewWithTag:TRANSPARENT_NAVBAR_TAG];
  319. if (transparentView)
  320. {
  321. [transparentView removeFromSuperview];
  322. [viewController.navigationController.navigationBar setBackgroundImage:self.originalNavBarImages[@"bgImage"] forBarMetrics:UIBarMetricsDefault];
  323. viewController.navigationController.navigationBar.shadowImage = self.originalNavBarImages[@"shadowImage"];
  324. self.originalNavBarImages = nil;
  325. }
  326. }
  327. };
  328. if (!self.transitionCoordinator || self.transitionCoordinator.initiallyInteractive || !navBarTransparentBool || appeared) {
  329. action();
  330. } else {
  331. UIView* backgroundView = [self.navigationController.navigationBar valueForKey:@"backgroundView"];
  332. CGFloat originalAlpha = backgroundView.alpha;
  333. backgroundView.alpha = navBarTransparentBool ? 0.0 : 1.0;
  334. [self.transitionCoordinator animateAlongsideTransition:nil completion:^(id<UIViewControllerTransitionCoordinatorContext> _Nonnull context) {
  335. action();
  336. backgroundView.alpha = originalAlpha;
  337. }];
  338. }
  339. NSNumber *autoAdjustsScrollViewInsets = self.navigatorStyle[@"autoAdjustScrollViewInsets"];
  340. viewController.automaticallyAdjustsScrollViewInsets = autoAdjustsScrollViewInsets ? [autoAdjustsScrollViewInsets boolValue] : false;
  341. NSNumber *navBarTranslucent = self.navigatorStyle[@"navBarTranslucent"];
  342. BOOL navBarTranslucentBool = navBarTranslucent ? [navBarTranslucent boolValue] : NO;
  343. if (navBarTranslucentBool || navBarBlurBool) {
  344. viewController.navigationController.navigationBar.translucent = YES;
  345. } else {
  346. viewController.navigationController.navigationBar.translucent = NO;
  347. }
  348. NSNumber *extendedLayoutIncludesOpaqueBars = self.navigatorStyle[@"extendedLayoutIncludesOpaqueBars"];
  349. BOOL extendedLayoutIncludesOpaqueBarsBool = extendedLayoutIncludesOpaqueBars ? [extendedLayoutIncludesOpaqueBars boolValue] : NO;
  350. viewController.extendedLayoutIncludesOpaqueBars = extendedLayoutIncludesOpaqueBarsBool;
  351. NSNumber *drawUnderNavBar = self.navigatorStyle[@"drawUnderNavBar"];
  352. BOOL drawUnderNavBarBool = drawUnderNavBar ? [drawUnderNavBar boolValue] : NO;
  353. if (drawUnderNavBarBool) {
  354. viewController.edgesForExtendedLayout |= UIRectEdgeTop;
  355. }
  356. else {
  357. viewController.edgesForExtendedLayout &= ~UIRectEdgeTop;
  358. }
  359. NSNumber *drawUnderTabBar = self.navigatorStyle[@"drawUnderTabBar"];
  360. BOOL drawUnderTabBarBool = drawUnderTabBar ? [drawUnderTabBar boolValue] : NO;
  361. if (drawUnderTabBarBool) {
  362. viewController.edgesForExtendedLayout |= UIRectEdgeBottom;
  363. } else {
  364. viewController.edgesForExtendedLayout &= ~UIRectEdgeBottom;
  365. }
  366. NSNumber *removeNavBarBorder = self.navigatorStyle[@"navBarNoBorder"];
  367. BOOL removeNavBarBorderBool = removeNavBarBorder ? [removeNavBarBorder boolValue] : NO;
  368. if (removeNavBarBorderBool) {
  369. self.navBarHairlineImageView.hidden = YES;
  370. } else {
  371. self.navBarHairlineImageView.hidden = NO;
  372. }
  373. //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
  374. //overriding the delegate of the gesture prevents this from happening while keeping the gesture intact (another option was to disable it completely by demand)
  375. self.originalInteractivePopGestureDelegate = nil;
  376. if (self.navigationController != nil && self.navigationController.interactivePopGestureRecognizer != nil)
  377. {
  378. id <UIGestureRecognizerDelegate> interactivePopGestureRecognizer = self.navigationController.interactivePopGestureRecognizer.delegate;
  379. if (interactivePopGestureRecognizer != nil)
  380. {
  381. self.originalInteractivePopGestureDelegate = interactivePopGestureRecognizer;
  382. self.navigationController.interactivePopGestureRecognizer.delegate = self;
  383. }
  384. }
  385. }
  386. -(void)storeOriginalNavBarImages {
  387. NSMutableDictionary *originalNavBarImages = [@{} mutableCopy];
  388. UIImage *bgImage = [self.navigationController.navigationBar backgroundImageForBarMetrics:UIBarMetricsDefault];
  389. if (bgImage != nil) {
  390. originalNavBarImages[@"bgImage"] = bgImage;
  391. }
  392. UIImage *shadowImage = self.navigationController.navigationBar.shadowImage;
  393. if (shadowImage != nil) {
  394. originalNavBarImages[@"shadowImage"] = shadowImage;
  395. }
  396. self.originalNavBarImages = originalNavBarImages;
  397. }
  398. -(void)setStyleOnDisappear
  399. {
  400. self.navBarHairlineImageView.hidden = NO;
  401. if (self.navigationController != nil && self.navigationController.interactivePopGestureRecognizer != nil && self.originalInteractivePopGestureDelegate != nil)
  402. {
  403. self.navigationController.interactivePopGestureRecognizer.delegate = self.originalInteractivePopGestureDelegate;
  404. self.originalInteractivePopGestureDelegate = nil;
  405. }
  406. }
  407. // only styles that can't be set on willAppear should be set here
  408. - (void)setStyleOnInit
  409. {
  410. NSNumber *tabBarHidden = self.navigatorStyle[@"tabBarHidden"];
  411. BOOL tabBarHiddenBool = tabBarHidden ? [tabBarHidden boolValue] : NO;
  412. if (tabBarHiddenBool) {
  413. self._hidesBottomBarWhenPushed = YES;
  414. } else {
  415. self._hidesBottomBarWhenPushed = NO;
  416. }
  417. NSNumber *statusBarHideWithNavBar = self.navigatorStyle[@"statusBarHideWithNavBar"];
  418. BOOL statusBarHideWithNavBarBool = statusBarHideWithNavBar ? [statusBarHideWithNavBar boolValue] : NO;
  419. if (statusBarHideWithNavBarBool) {
  420. self._statusBarHideWithNavBar = YES;
  421. } else {
  422. self._statusBarHideWithNavBar = NO;
  423. }
  424. NSNumber *statusBarHidden = self.navigatorStyle[@"statusBarHidden"];
  425. BOOL statusBarHiddenBool = statusBarHidden ? [statusBarHidden boolValue] : NO;
  426. if (statusBarHiddenBool) {
  427. self._statusBarHidden = YES;
  428. } else {
  429. self._statusBarHidden = NO;
  430. }
  431. }
  432. - (BOOL)hidesBottomBarWhenPushed
  433. {
  434. if (!self._hidesBottomBarWhenPushed) return NO;
  435. return (self.navigationController.topViewController == self);
  436. }
  437. - (BOOL)prefersStatusBarHidden
  438. {
  439. if (self._statusBarHidden) {
  440. return YES;
  441. }
  442. if (self._statusBarHideWithNavBar) {
  443. return self.navigationController.isNavigationBarHidden;
  444. } else {
  445. return NO;
  446. }
  447. }
  448. - (void)setNavBarVisibilityChange:(BOOL)animated {
  449. [self.navigationController setNavigationBarHidden:[self.navigatorStyle[@"navBarHidden"] boolValue] animated:animated];
  450. }
  451. - (UIStatusBarStyle)preferredStatusBarStyle
  452. {
  453. if (self._statusBarTextColorSchemeLight){
  454. return UIStatusBarStyleLightContent;
  455. } else {
  456. return UIStatusBarStyleDefault;
  457. }
  458. }
  459. - (UIImageView *)findHairlineImageViewUnder:(UIView *)view {
  460. if ([view isKindOfClass:UIImageView.class] && view.bounds.size.height <= 1.0) {
  461. return (UIImageView *)view;
  462. }
  463. for (UIView *subview in view.subviews) {
  464. UIImageView *imageView = [self findHairlineImageViewUnder:subview];
  465. if (imageView) {
  466. return imageView;
  467. }
  468. }
  469. return nil;
  470. }
  471. -(void)addExternalVCIfNecessary:(NSDictionary*)props
  472. {
  473. NSString *externalScreenClass = props[@"externalNativeScreenClass"];
  474. if (externalScreenClass != nil)
  475. {
  476. Class class = NSClassFromString(externalScreenClass);
  477. if (class != NULL)
  478. {
  479. id obj = [[class alloc] init];
  480. if (obj != nil && [obj isKindOfClass:[UIViewController class]] && [obj conformsToProtocol:@protocol(RCCExternalViewControllerProtocol)])
  481. {
  482. ((id <RCCExternalViewControllerProtocol>)obj).controllerDelegate = self;
  483. [obj setProps:props[@"externalNativeScreenProps"]];
  484. UIViewController *viewController = (UIViewController*)obj;
  485. [self addChildViewController:viewController];
  486. viewController.view.frame = self.view.bounds;
  487. [self.view addSubview:viewController.view];
  488. [viewController didMoveToParentViewController:self];
  489. }
  490. else
  491. {
  492. NSLog(@"addExternalVCIfNecessary: could not create instance. Make sure that your class is a UIViewController whihc confirms to RCCExternalViewControllerProtocol");
  493. }
  494. }
  495. else
  496. {
  497. NSLog(@"addExternalVCIfNecessary: could not create class from string. Check that the proper class name wass passed in ExternalNativeScreenClass");
  498. }
  499. }
  500. }
  501. #pragma mark - NewRelic
  502. - (NSString*) customNewRelicInteractionName
  503. {
  504. NSString *interactionName = nil;
  505. if (self.view != nil && [self.view isKindOfClass:[RCTRootView class]])
  506. {
  507. NSString *moduleName = ((RCTRootView*)self.view).moduleName;
  508. if(moduleName != nil)
  509. {
  510. interactionName = [NSString stringWithFormat:@"RCCViewController: %@", moduleName];
  511. }
  512. }
  513. if (interactionName == nil)
  514. {
  515. interactionName = [NSString stringWithFormat:@"RCCViewController with title: %@", self.title];
  516. }
  517. return interactionName;
  518. }
  519. @end