react-native-navigation的迁移库

RCCViewController.m 24KB

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