react-native-navigation的迁移库

RCCViewController.m 27KB

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