react-native-navigation的迁移库

RCCViewController.m 27KB

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