react-native-navigation的迁移库

RCCViewController.m 25KB

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