react-native-navigation的迁移库

RCCViewController.m 26KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635
  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 *navBarBackgroundColor = self.navigatorStyle[@"navBarBackgroundColor"];
  191. if (navBarBackgroundColor) {
  192. UIColor *color = navBarBackgroundColor != (id)[NSNull null] ? [RCTConvert UIColor:navBarBackgroundColor] : nil;
  193. viewController.navigationController.navigationBar.barTintColor = color;
  194. } else {
  195. viewController.navigationController.navigationBar.barTintColor = nil;
  196. }
  197. NSMutableDictionary *titleTextAttributes = [RCTHelpers textAttributesFromDictionary:self.navigatorStyle withPrefix:@"navBarText" baseFont:[UIFont boldSystemFontOfSize:17]];
  198. [self.navigationController.navigationBar setTitleTextAttributes:titleTextAttributes];
  199. if (self.navigationItem.titleView && [self.navigationItem.titleView isKindOfClass:[RCCTitleView class]]) {
  200. RCCTitleView *titleView = (RCCTitleView *)self.navigationItem.titleView;
  201. RCCTitleViewHelper *helper = [[RCCTitleViewHelper alloc] init:viewController navigationController:viewController.navigationController title:titleView.titleLabel.text subtitle:titleView.subtitleLabel.text titleImageData:nil];
  202. [helper setup:self.navigatorStyle];
  203. }
  204. NSMutableDictionary *navButtonTextAttributes = [RCTHelpers textAttributesFromDictionary:self.navigatorStyle withPrefix:@"navBarButton"];
  205. if (navButtonTextAttributes.allKeys.count > 0) {
  206. for (UIBarButtonItem *item in viewController.navigationItem.leftBarButtonItems) {
  207. [item setTitleTextAttributes:navButtonTextAttributes forState:UIControlStateNormal];
  208. }
  209. for (UIBarButtonItem *item in viewController.navigationItem.rightBarButtonItems) {
  210. [item setTitleTextAttributes:navButtonTextAttributes forState:UIControlStateNormal];
  211. }
  212. // At the moment, this seems to be the only thing that gets the back button correctly
  213. [navButtonTextAttributes removeObjectForKey:NSForegroundColorAttributeName];
  214. [[UIBarButtonItem appearance] setTitleTextAttributes:navButtonTextAttributes forState:UIControlStateNormal];
  215. // [viewController.navigationItem.backBarButtonItem setTitleTextAttributes:navButtonTextAttributes forState:UIControlStateNormal];
  216. }
  217. NSString *navBarButtonColor = self.navigatorStyle[@"navBarButtonColor"];
  218. if (navBarButtonColor) {
  219. UIColor *color = navBarButtonColor != (id)[NSNull null] ? [RCTConvert UIColor:navBarButtonColor] : nil;
  220. viewController.navigationController.navigationBar.tintColor = color;
  221. } else
  222. {
  223. viewController.navigationController.navigationBar.tintColor = nil;
  224. }
  225. BOOL viewControllerBasedStatusBar = false;
  226. NSObject *viewControllerBasedStatusBarAppearance = [[NSBundle mainBundle] infoDictionary][@"UIViewControllerBasedStatusBarAppearance"];
  227. if (viewControllerBasedStatusBarAppearance && [viewControllerBasedStatusBarAppearance isKindOfClass:[NSNumber class]]) {
  228. viewControllerBasedStatusBar = [(NSNumber *)viewControllerBasedStatusBarAppearance boolValue];
  229. }
  230. NSString *statusBarTextColorSchemeSingleScreen = self.navigatorStyle[@"statusBarTextColorSchemeSingleScreen"];
  231. NSString *statusBarTextColorScheme = self.navigatorStyle[@"statusBarTextColorScheme"];
  232. NSString *finalColorScheme = statusBarTextColorSchemeSingleScreen ? : statusBarTextColorScheme;
  233. if (finalColorScheme && [finalColorScheme isEqualToString:@"light"]) {
  234. if (!statusBarTextColorSchemeSingleScreen) {
  235. viewController.navigationController.navigationBar.barStyle = UIBarStyleBlack;
  236. }
  237. self._statusBarTextColorSchemeLight = true;
  238. if (!viewControllerBasedStatusBarAppearance) {
  239. [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
  240. }
  241. [viewController setNeedsStatusBarAppearanceUpdate];
  242. } else {
  243. if (!statusBarTextColorSchemeSingleScreen) {
  244. viewController.navigationController.navigationBar.barStyle = UIBarStyleDefault;
  245. }
  246. self._statusBarTextColorSchemeLight = false;
  247. if (!viewControllerBasedStatusBarAppearance) {
  248. [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
  249. }
  250. [viewController setNeedsStatusBarAppearanceUpdate];
  251. }
  252. NSNumber *navBarHidden = self.navigatorStyle[@"navBarHidden"];
  253. BOOL navBarHiddenBool = navBarHidden ? [navBarHidden boolValue] : NO;
  254. if (viewController.navigationController.navigationBarHidden != navBarHiddenBool) {
  255. [viewController.navigationController setNavigationBarHidden:navBarHiddenBool animated:YES];
  256. }
  257. NSNumber *navBarHideOnScroll = self.navigatorStyle[@"navBarHideOnScroll"];
  258. BOOL navBarHideOnScrollBool = navBarHideOnScroll ? [navBarHideOnScroll boolValue] : NO;
  259. if (navBarHideOnScrollBool) {
  260. viewController.navigationController.hidesBarsOnSwipe = YES;
  261. } else {
  262. viewController.navigationController.hidesBarsOnSwipe = NO;
  263. }
  264. NSNumber *statusBarBlur = self.navigatorStyle[@"statusBarBlur"];
  265. BOOL statusBarBlurBool = statusBarBlur ? [statusBarBlur boolValue] : NO;
  266. if (statusBarBlurBool && ![viewController.view viewWithTag:BLUR_STATUS_TAG]) {
  267. UIVisualEffectView *blur = [[UIVisualEffectView alloc] initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleLight]];
  268. blur.frame = [[UIApplication sharedApplication] statusBarFrame];
  269. blur.tag = BLUR_STATUS_TAG;
  270. [viewController.view insertSubview:blur atIndex:0];
  271. }
  272. NSNumber *navBarBlur = self.navigatorStyle[@"navBarBlur"];
  273. BOOL navBarBlurBool = navBarBlur ? [navBarBlur boolValue] : NO;
  274. if (navBarBlurBool) {
  275. if (![viewController.navigationController.navigationBar viewWithTag:BLUR_NAVBAR_TAG]) {
  276. [self storeOriginalNavBarImages];
  277. [viewController.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
  278. viewController.navigationController.navigationBar.shadowImage = [UIImage new];
  279. UIVisualEffectView *blur = [[UIVisualEffectView alloc] initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleLight]];
  280. CGRect statusBarFrame = [[UIApplication sharedApplication] statusBarFrame];
  281. blur.frame = CGRectMake(0, -1 * statusBarFrame.size.height, viewController.navigationController.navigationBar.frame.size.width, viewController.navigationController.navigationBar.frame.size.height + statusBarFrame.size.height);
  282. blur.userInteractionEnabled = NO;
  283. blur.tag = BLUR_NAVBAR_TAG;
  284. [viewController.navigationController.navigationBar insertSubview:blur atIndex:0];
  285. [viewController.navigationController.navigationBar sendSubviewToBack:blur];
  286. }
  287. } else {
  288. UIView *blur = [viewController.navigationController.navigationBar viewWithTag:BLUR_NAVBAR_TAG];
  289. if (blur) {
  290. [blur removeFromSuperview];
  291. [viewController.navigationController.navigationBar setBackgroundImage:self.originalNavBarImages[@"bgImage"] forBarMetrics:UIBarMetricsDefault];
  292. viewController.navigationController.navigationBar.shadowImage = self.originalNavBarImages[@"shadowImage"];
  293. self.originalNavBarImages = nil;
  294. }
  295. }
  296. NSNumber *navBarTransparent = self.navigatorStyle[@"navBarTransparent"];
  297. BOOL navBarTransparentBool = navBarTransparent ? [navBarTransparent boolValue] : NO;
  298. void (^action)() = ^ {
  299. if (navBarTransparentBool)
  300. {
  301. if (![viewController.navigationController.navigationBar viewWithTag:TRANSPARENT_NAVBAR_TAG])
  302. {
  303. [self storeOriginalNavBarImages];
  304. [viewController.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
  305. viewController.navigationController.navigationBar.shadowImage = [UIImage new];
  306. UIView *transparentView = [[UIView alloc] initWithFrame:CGRectZero];
  307. transparentView.tag = TRANSPARENT_NAVBAR_TAG;
  308. [viewController.navigationController.navigationBar insertSubview:transparentView atIndex:0];
  309. }
  310. }
  311. else
  312. {
  313. UIView *transparentView = [viewController.navigationController.navigationBar viewWithTag:TRANSPARENT_NAVBAR_TAG];
  314. if (transparentView)
  315. {
  316. [transparentView removeFromSuperview];
  317. [viewController.navigationController.navigationBar setBackgroundImage:self.originalNavBarImages[@"bgImage"] forBarMetrics:UIBarMetricsDefault];
  318. viewController.navigationController.navigationBar.shadowImage = self.originalNavBarImages[@"shadowImage"];
  319. self.originalNavBarImages = nil;
  320. }
  321. }
  322. };
  323. if (!self.transitionCoordinator || self.transitionCoordinator.initiallyInteractive || !navBarTransparentBool || appeared) {
  324. action();
  325. } else {
  326. UIView* backgroundView = [self.navigationController.navigationBar valueForKey:@"backgroundView"];
  327. CGFloat originalAlpha = backgroundView.alpha;
  328. backgroundView.alpha = navBarTransparentBool ? 0.0 : 1.0;
  329. [self.transitionCoordinator animateAlongsideTransition:nil completion:^(id<UIViewControllerTransitionCoordinatorContext> _Nonnull context) {
  330. action();
  331. backgroundView.alpha = originalAlpha;
  332. }];
  333. }
  334. NSNumber *autoAdjustsScrollViewInsets = self.navigatorStyle[@"autoAdjustScrollViewInsets"];
  335. viewController.automaticallyAdjustsScrollViewInsets = autoAdjustsScrollViewInsets ? [autoAdjustsScrollViewInsets boolValue] : false;
  336. NSNumber *navBarTranslucent = self.navigatorStyle[@"navBarTranslucent"];
  337. BOOL navBarTranslucentBool = navBarTranslucent ? [navBarTranslucent boolValue] : NO;
  338. if (navBarTranslucentBool || navBarBlurBool) {
  339. viewController.navigationController.navigationBar.translucent = YES;
  340. } else {
  341. viewController.navigationController.navigationBar.translucent = NO;
  342. }
  343. NSNumber *extendedLayoutIncludesOpaqueBars = self.navigatorStyle[@"extendedLayoutIncludesOpaqueBars"];
  344. BOOL extendedLayoutIncludesOpaqueBarsBool = extendedLayoutIncludesOpaqueBars ? [extendedLayoutIncludesOpaqueBars boolValue] : NO;
  345. viewController.extendedLayoutIncludesOpaqueBars = extendedLayoutIncludesOpaqueBarsBool;
  346. NSNumber *drawUnderNavBar = self.navigatorStyle[@"drawUnderNavBar"];
  347. BOOL drawUnderNavBarBool = drawUnderNavBar ? [drawUnderNavBar boolValue] : NO;
  348. if (drawUnderNavBarBool) {
  349. viewController.edgesForExtendedLayout |= UIRectEdgeTop;
  350. }
  351. else {
  352. viewController.edgesForExtendedLayout &= ~UIRectEdgeTop;
  353. }
  354. NSNumber *drawUnderTabBar = self.navigatorStyle[@"drawUnderTabBar"];
  355. BOOL drawUnderTabBarBool = drawUnderTabBar ? [drawUnderTabBar boolValue] : NO;
  356. if (drawUnderTabBarBool) {
  357. viewController.edgesForExtendedLayout |= UIRectEdgeBottom;
  358. } else {
  359. viewController.edgesForExtendedLayout &= ~UIRectEdgeBottom;
  360. }
  361. NSNumber *removeNavBarBorder = self.navigatorStyle[@"navBarNoBorder"];
  362. BOOL removeNavBarBorderBool = removeNavBarBorder ? [removeNavBarBorder boolValue] : NO;
  363. if (removeNavBarBorderBool) {
  364. self.navBarHairlineImageView.hidden = YES;
  365. } else {
  366. self.navBarHairlineImageView.hidden = NO;
  367. }
  368. //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
  369. //overriding the delegate of the gesture prevents this from happening while keeping the gesture intact (another option was to disable it completely by demand)
  370. self.originalInteractivePopGestureDelegate = nil;
  371. if (self.navigationController != nil && self.navigationController.interactivePopGestureRecognizer != nil)
  372. {
  373. id <UIGestureRecognizerDelegate> interactivePopGestureRecognizer = self.navigationController.interactivePopGestureRecognizer.delegate;
  374. if (interactivePopGestureRecognizer != nil)
  375. {
  376. self.originalInteractivePopGestureDelegate = interactivePopGestureRecognizer;
  377. self.navigationController.interactivePopGestureRecognizer.delegate = self;
  378. }
  379. }
  380. }
  381. -(void)storeOriginalNavBarImages {
  382. NSMutableDictionary *originalNavBarImages = [@{} mutableCopy];
  383. UIImage *bgImage = [self.navigationController.navigationBar backgroundImageForBarMetrics:UIBarMetricsDefault];
  384. if (bgImage != nil) {
  385. originalNavBarImages[@"bgImage"] = bgImage;
  386. }
  387. UIImage *shadowImage = self.navigationController.navigationBar.shadowImage;
  388. if (shadowImage != nil) {
  389. originalNavBarImages[@"shadowImage"] = shadowImage;
  390. }
  391. self.originalNavBarImages = originalNavBarImages;
  392. }
  393. -(void)setStyleOnDisappear
  394. {
  395. self.navBarHairlineImageView.hidden = NO;
  396. if (self.navigationController != nil && self.navigationController.interactivePopGestureRecognizer != nil && self.originalInteractivePopGestureDelegate != nil)
  397. {
  398. self.navigationController.interactivePopGestureRecognizer.delegate = self.originalInteractivePopGestureDelegate;
  399. self.originalInteractivePopGestureDelegate = nil;
  400. }
  401. }
  402. // only styles that can't be set on willAppear should be set here
  403. - (void)setStyleOnInit
  404. {
  405. NSNumber *tabBarHidden = self.navigatorStyle[@"tabBarHidden"];
  406. BOOL tabBarHiddenBool = tabBarHidden ? [tabBarHidden boolValue] : NO;
  407. if (tabBarHiddenBool) {
  408. self._hidesBottomBarWhenPushed = YES;
  409. } else {
  410. self._hidesBottomBarWhenPushed = NO;
  411. }
  412. NSNumber *statusBarHideWithNavBar = self.navigatorStyle[@"statusBarHideWithNavBar"];
  413. BOOL statusBarHideWithNavBarBool = statusBarHideWithNavBar ? [statusBarHideWithNavBar boolValue] : NO;
  414. if (statusBarHideWithNavBarBool) {
  415. self._statusBarHideWithNavBar = YES;
  416. } else {
  417. self._statusBarHideWithNavBar = NO;
  418. }
  419. NSNumber *statusBarHidden = self.navigatorStyle[@"statusBarHidden"];
  420. BOOL statusBarHiddenBool = statusBarHidden ? [statusBarHidden boolValue] : NO;
  421. if (statusBarHiddenBool) {
  422. self._statusBarHidden = YES;
  423. } else {
  424. self._statusBarHidden = NO;
  425. }
  426. }
  427. - (BOOL)hidesBottomBarWhenPushed
  428. {
  429. if (!self._hidesBottomBarWhenPushed) return NO;
  430. return (self.navigationController.topViewController == self);
  431. }
  432. - (BOOL)prefersStatusBarHidden
  433. {
  434. if (self._statusBarHidden) {
  435. return YES;
  436. }
  437. if (self._statusBarHideWithNavBar) {
  438. return self.navigationController.isNavigationBarHidden;
  439. } else {
  440. return NO;
  441. }
  442. }
  443. - (void)setNavBarVisibilityChange:(BOOL)animated {
  444. [self.navigationController setNavigationBarHidden:[self.navigatorStyle[@"navBarHidden"] boolValue] animated:animated];
  445. }
  446. - (UIStatusBarStyle)preferredStatusBarStyle
  447. {
  448. if (self._statusBarTextColorSchemeLight){
  449. return UIStatusBarStyleLightContent;
  450. } else {
  451. return UIStatusBarStyleDefault;
  452. }
  453. }
  454. - (UIImageView *)findHairlineImageViewUnder:(UIView *)view {
  455. if ([view isKindOfClass:UIImageView.class] && view.bounds.size.height <= 1.0) {
  456. return (UIImageView *)view;
  457. }
  458. for (UIView *subview in view.subviews) {
  459. UIImageView *imageView = [self findHairlineImageViewUnder:subview];
  460. if (imageView) {
  461. return imageView;
  462. }
  463. }
  464. return nil;
  465. }
  466. -(void)addExternalVCIfNecessary:(NSDictionary*)props
  467. {
  468. NSString *externalScreenClass = props[@"externalNativeScreenClass"];
  469. if (externalScreenClass != nil)
  470. {
  471. Class class = NSClassFromString(externalScreenClass);
  472. if (class != NULL)
  473. {
  474. id obj = [[class alloc] init];
  475. if (obj != nil && [obj isKindOfClass:[UIViewController class]] && [obj conformsToProtocol:@protocol(RCCExternalViewControllerProtocol)])
  476. {
  477. ((id <RCCExternalViewControllerProtocol>)obj).controllerDelegate = self;
  478. [obj setProps:props[@"externalNativeScreenProps"]];
  479. UIViewController *viewController = (UIViewController*)obj;
  480. [self addChildViewController:viewController];
  481. viewController.view.frame = self.view.bounds;
  482. [self.view addSubview:viewController.view];
  483. [viewController didMoveToParentViewController:self];
  484. }
  485. else
  486. {
  487. NSLog(@"addExternalVCIfNecessary: could not create instance. Make sure that your class is a UIViewController whihc confirms to RCCExternalViewControllerProtocol");
  488. }
  489. }
  490. else
  491. {
  492. NSLog(@"addExternalVCIfNecessary: could not create class from string. Check that the proper class name wass passed in ExternalNativeScreenClass");
  493. }
  494. }
  495. }
  496. #pragma mark - NewRelic
  497. - (NSString*) customNewRelicInteractionName
  498. {
  499. NSString *interactionName = nil;
  500. if (self.view != nil && [self.view isKindOfClass:[RCTRootView class]])
  501. {
  502. NSString *moduleName = ((RCTRootView*)self.view).moduleName;
  503. if(moduleName != nil)
  504. {
  505. interactionName = [NSString stringWithFormat:@"RCCViewController: %@", moduleName];
  506. }
  507. }
  508. if (interactionName == nil)
  509. {
  510. interactionName = [NSString stringWithFormat:@"RCCViewController with title: %@", self.title];
  511. }
  512. return interactionName;
  513. }
  514. @end