react-native-navigation的迁移库

RCCViewController.m 25KB

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