react-native-navigation的迁移库

RCCViewController.m 32KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808
  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. if([controller isKindOfClass:[RCCViewController class]])
  81. {
  82. ((RCCViewController*)controller).controllerId = componentId;
  83. }
  84. }
  85. // set background image at root level
  86. NSString *rootBackgroundImageName = props[@"style"][@"rootBackgroundImageName"];
  87. if (rootBackgroundImageName) {
  88. UIImage *image = [UIImage imageNamed: rootBackgroundImageName];
  89. UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
  90. [controller.view insertSubview:imageView atIndex:0];
  91. }
  92. return controller;
  93. }
  94. -(NSDictionary*)addCommandTypeAndTimestampIfExists:(NSDictionary*)globalProps passProps:(NSDictionary*)passProps {
  95. NSMutableDictionary *modifiedPassProps = [NSMutableDictionary dictionaryWithDictionary:passProps];
  96. NSString *commandType = globalProps[GLOBAL_SCREEN_ACTION_COMMAND_TYPE];
  97. if (commandType) {
  98. modifiedPassProps[GLOBAL_SCREEN_ACTION_COMMAND_TYPE] = commandType;
  99. }
  100. NSString *timestamp = globalProps[GLOBAL_SCREEN_ACTION_TIMESTAMP];
  101. if (timestamp) {
  102. modifiedPassProps[GLOBAL_SCREEN_ACTION_TIMESTAMP] = timestamp;
  103. }
  104. return modifiedPassProps;
  105. }
  106. - (instancetype)initWithProps:(NSDictionary *)props children:(NSArray *)children globalProps:(NSDictionary *)globalProps bridge:(RCTBridge *)bridge
  107. {
  108. NSString *component = props[@"component"];
  109. if (!component) return nil;
  110. NSDictionary *passProps = props[@"passProps"];
  111. NSDictionary *navigatorStyle = props[@"style"];
  112. NSMutableDictionary *mergedProps = [NSMutableDictionary dictionaryWithDictionary:globalProps];
  113. [mergedProps addEntriesFromDictionary:passProps];
  114. RCTRootView *reactView = [[RCTRootView alloc] initWithBridge:bridge moduleName:component initialProperties:mergedProps];
  115. if (!reactView) return nil;
  116. self = [super init];
  117. if (!self) return nil;
  118. [self commonInit:reactView navigatorStyle:navigatorStyle props:props];
  119. self.navigationController.interactivePopGestureRecognizer.delegate = self;
  120. return self;
  121. }
  122. - (instancetype)initWithComponent:(NSString *)component passProps:(NSDictionary *)passProps navigatorStyle:(NSDictionary*)navigatorStyle globalProps:(NSDictionary *)globalProps bridge:(RCTBridge *)bridge
  123. {
  124. NSMutableDictionary *mergedProps = [NSMutableDictionary dictionaryWithDictionary:globalProps];
  125. [mergedProps addEntriesFromDictionary:passProps];
  126. RCTRootView *reactView = [[RCTRootView alloc] initWithBridge:bridge moduleName:component initialProperties:mergedProps];
  127. if (!reactView) return nil;
  128. self = [super init];
  129. if (!self) return nil;
  130. NSDictionary *modifiedPassProps = [self addCommandTypeAndTimestampIfExists:globalProps passProps:passProps];
  131. [self commonInit:reactView navigatorStyle:navigatorStyle props:modifiedPassProps];
  132. return self;
  133. }
  134. - (void)commonInit:(RCTRootView*)reactView navigatorStyle:(NSDictionary*)navigatorStyle props:(NSDictionary*)props
  135. {
  136. self.view = reactView;
  137. self.edgesForExtendedLayout = UIRectEdgeNone; // default
  138. self.automaticallyAdjustsScrollViewInsets = NO; // default
  139. self.navigatorStyle = [NSMutableDictionary dictionaryWithDictionary:[[RCCManager sharedInstance] getAppStyle]];
  140. [self.navigatorStyle addEntriesFromDictionary:navigatorStyle];
  141. [self setStyleOnInit];
  142. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onRNReload) name:RCTJavaScriptWillStartLoadingNotification object:nil];
  143. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onCancelReactTouches) name:RCCViewControllerCancelReactTouchesNotification object:nil];
  144. self.commandType = props[GLOBAL_SCREEN_ACTION_COMMAND_TYPE];
  145. self.timestamp = props[GLOBAL_SCREEN_ACTION_TIMESTAMP];
  146. // In order to support 3rd party native ViewControllers, we support passing a class name as a prop named `ExternalNativeScreenClass`
  147. // In this case, we create an instance and add it as a child ViewController which preserves the VC lifecycle.
  148. // In case some props are necessary in the native ViewController, the ExternalNativeScreenProps can be used to pass them
  149. [self addExternalVCIfNecessary:props];
  150. }
  151. - (void)dealloc
  152. {
  153. [[NSNotificationCenter defaultCenter] removeObserver:self];
  154. self.view = nil;
  155. }
  156. -(void)onRNReload
  157. {
  158. [[NSNotificationCenter defaultCenter] removeObserver:self];
  159. self.view = nil;
  160. }
  161. -(void)onCancelReactTouches
  162. {
  163. if ([self.view isKindOfClass:[RCTRootView class]]){
  164. [(RCTRootView*)self.view cancelTouches];
  165. }
  166. }
  167. - (void)sendScreenChangedEvent:(NSString *)eventName
  168. {
  169. if ([self.view isKindOfClass:[RCTRootView class]]){
  170. RCTRootView *rootView = (RCTRootView *)self.view;
  171. if (rootView.appProperties && rootView.appProperties[@"navigatorEventID"]) {
  172. [[[RCCManager sharedInstance] getBridge].eventDispatcher sendAppEventWithName:rootView.appProperties[@"navigatorEventID"] body:@
  173. {
  174. @"type": @"ScreenChangedEvent",
  175. @"id": eventName
  176. }];
  177. }
  178. }
  179. }
  180. - (void)sendGlobalScreenEvent:(NSString *)eventName endTimestampString:(NSString *)endTimestampStr shouldReset:(BOOL)shouldReset {
  181. if (!self.commandType) return;
  182. if ([self.view isKindOfClass:[RCTRootView class]]){
  183. NSString *screenName = [((RCTRootView*)self.view) moduleName];
  184. [[[RCCManager sharedInstance] getBridge].eventDispatcher sendAppEventWithName:eventName body:@
  185. {
  186. @"commandType": self.commandType ? self.commandType : @"",
  187. @"screen": screenName ? screenName : @"",
  188. @"startTime": self.timestamp ? self.timestamp : @"",
  189. @"endTime": endTimestampStr ? endTimestampStr : @""
  190. }];
  191. if (shouldReset) {
  192. self.commandType = nil;
  193. self.timestamp = nil;
  194. }
  195. }
  196. }
  197. -(BOOL)isDisappearTriggeredFromPop:(NSString *)eventName {
  198. NSArray *navigationViewControllers = self.navigationController.viewControllers;
  199. if (navigationViewControllers.lastObject == self || [navigationViewControllers indexOfObject:self] == NSNotFound) {
  200. return YES;
  201. }
  202. return NO;
  203. }
  204. - (NSString *)getTimestampString {
  205. long long milliseconds = (long long)([[NSDate date] timeIntervalSince1970] * 1000.0);
  206. return [NSString stringWithFormat:@"%lld", milliseconds];
  207. }
  208. // This is walk around for React-Native bug.
  209. // https://github.com/wix/react-native-navigation/issues/1446
  210. //
  211. // Buttons in ScrollView after changing route/pushing/showing modal
  212. // while there is a momentum scroll are not clickable.
  213. // Back to normal after user start scroll with momentum
  214. - (void)_traverseAndCall:(UIView*)view
  215. {
  216. if([view isKindOfClass:[UIScrollView class]] && ([[(UIScrollView*)view delegate] respondsToSelector:@selector(scrollViewDidEndDecelerating:)]) ) {
  217. [[(UIScrollView*)view delegate] scrollViewDidEndDecelerating:(id)view];
  218. }
  219. [view.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  220. [self _traverseAndCall:obj];
  221. }];
  222. }
  223. // fix iOS11 safeArea - https://github.com/facebook/react-native/issues/15681
  224. // rnn issue - https://github.com/wix/react-native-navigation/issues/1858
  225. - (void)_traverseAndFixScrollViewSafeArea:(UIView *)view {
  226. #ifdef __IPHONE_11_0
  227. if ([view isKindOfClass:UIScrollView.class]) {
  228. [((UIScrollView*)view) setContentInsetAdjustmentBehavior:UIScrollViewContentInsetAdjustmentNever];
  229. }
  230. [view.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  231. [self _traverseAndFixScrollViewSafeArea:obj];
  232. }];
  233. #endif
  234. }
  235. - (void)viewDidAppear:(BOOL)animated
  236. {
  237. [super viewDidAppear:animated];
  238. [self sendGlobalScreenEvent:@"didAppear" endTimestampString:[self getTimestampString] shouldReset:YES];
  239. [self sendScreenChangedEvent:@"didAppear"];
  240. }
  241. - (void)viewWillAppear:(BOOL)animated
  242. {
  243. [super viewWillAppear:animated];
  244. [self _traverseAndFixScrollViewSafeArea:self.view];
  245. [self sendGlobalScreenEvent:@"willAppear" endTimestampString:[self getTimestampString] shouldReset:NO];
  246. [self sendScreenChangedEvent:@"willAppear"];
  247. [self setStyleOnAppear];
  248. }
  249. - (void)viewDidDisappear:(BOOL)animated
  250. {
  251. [self _traverseAndCall:self.view];
  252. [super viewDidDisappear:animated];
  253. [self sendGlobalScreenEvent:@"didDisappear" endTimestampString:[self getTimestampString] shouldReset:YES];
  254. [self sendScreenChangedEvent:@"didDisappear"];
  255. }
  256. - (void)viewWillDisappear:(BOOL)animated
  257. {
  258. [super viewWillDisappear:animated];
  259. [self sendGlobalScreenEvent:@"willDisappear" endTimestampString:[self getTimestampString] shouldReset:NO];
  260. [self sendScreenChangedEvent:@"willDisappear"];
  261. [self setStyleOnDisappear];
  262. }
  263. // most styles should be set here since when we pop a view controller that changed them
  264. // we want to reset the style to what we expect (so we need to reset on every willAppear)
  265. - (void)setStyleOnAppear
  266. {
  267. [self setStyleOnAppearForViewController:self appeared:false];
  268. }
  269. - (void)updateStyle
  270. {
  271. [self setStyleOnAppearForViewController:self appeared:true];
  272. }
  273. -(void)setStyleOnAppearForViewController:(UIViewController*)viewController appeared:(BOOL)appeared
  274. {
  275. NSString *screenBackgroundColor = self.navigatorStyle[@"screenBackgroundColor"];
  276. if (screenBackgroundColor) {
  277. UIColor *color = screenBackgroundColor != (id)[NSNull null] ? [RCTConvert UIColor:screenBackgroundColor] : nil;
  278. viewController.view.backgroundColor = color;
  279. }
  280. NSString *screenBackgroundImageName = self.navigatorStyle[@"screenBackgroundImageName"];
  281. if (screenBackgroundImageName) {
  282. UIImage *image = [UIImage imageNamed: screenBackgroundImageName];
  283. viewController.view.layer.contents = (__bridge id _Nullable)(image.CGImage);
  284. }
  285. NSString *navBarBackgroundColor = self.navigatorStyle[@"navBarBackgroundColor"];
  286. if (navBarBackgroundColor) {
  287. UIColor *color = navBarBackgroundColor != (id)[NSNull null] ? [RCTConvert UIColor:navBarBackgroundColor] : nil;
  288. viewController.navigationController.navigationBar.barTintColor = color;
  289. } else {
  290. viewController.navigationController.navigationBar.barTintColor = nil;
  291. }
  292. NSMutableDictionary *titleTextAttributes = [RCTHelpers textAttributesFromDictionary:self.navigatorStyle withPrefix:@"navBarText" baseFont:[UIFont boldSystemFontOfSize:17]];
  293. [self.navigationController.navigationBar setTitleTextAttributes:titleTextAttributes];
  294. if (self.navigationItem.titleView && [self.navigationItem.titleView isKindOfClass:[RCCTitleView class]]) {
  295. RCCTitleView *titleView = (RCCTitleView *)self.navigationItem.titleView;
  296. RCCTitleViewHelper *helper = [[RCCTitleViewHelper alloc] init:viewController navigationController:viewController.navigationController title:titleView.titleLabel.text subtitle:titleView.subtitleLabel.text titleImageData:nil isSetSubtitle:NO];
  297. [helper setup:self.navigatorStyle];
  298. }
  299. NSMutableDictionary *navButtonTextAttributes = [RCTHelpers textAttributesFromDictionary:self.navigatorStyle withPrefix:@"navBarButton"];
  300. NSMutableDictionary *leftNavButtonTextAttributes = [RCTHelpers textAttributesFromDictionary:self.navigatorStyle withPrefix:@"navBarLeftButton"];
  301. NSMutableDictionary *rightNavButtonTextAttributes = [RCTHelpers textAttributesFromDictionary:self.navigatorStyle withPrefix:@"navBarRightButton"];
  302. if (
  303. navButtonTextAttributes.allKeys.count > 0 ||
  304. leftNavButtonTextAttributes.allKeys.count > 0 ||
  305. rightNavButtonTextAttributes.allKeys.count > 0
  306. ) {
  307. for (UIBarButtonItem *item in viewController.navigationItem.leftBarButtonItems) {
  308. [item setTitleTextAttributes:navButtonTextAttributes forState:UIControlStateNormal];
  309. if (leftNavButtonTextAttributes.allKeys.count > 0) {
  310. [item setTitleTextAttributes:leftNavButtonTextAttributes forState:UIControlStateNormal];
  311. }
  312. }
  313. for (UIBarButtonItem *item in viewController.navigationItem.rightBarButtonItems) {
  314. [item setTitleTextAttributes:navButtonTextAttributes forState:UIControlStateNormal];
  315. if (rightNavButtonTextAttributes.allKeys.count > 0) {
  316. [item setTitleTextAttributes:rightNavButtonTextAttributes forState:UIControlStateNormal];
  317. }
  318. }
  319. // At the moment, this seems to be the only thing that gets the back button correctly
  320. [navButtonTextAttributes removeObjectForKey:NSForegroundColorAttributeName];
  321. [[UIBarButtonItem appearance] setTitleTextAttributes:navButtonTextAttributes forState:UIControlStateNormal];
  322. }
  323. NSString *navBarButtonColor = self.navigatorStyle[@"navBarButtonColor"];
  324. if (navBarButtonColor) {
  325. UIColor *color = navBarButtonColor != (id)[NSNull null] ? [RCTConvert UIColor:navBarButtonColor] : nil;
  326. viewController.navigationController.navigationBar.tintColor = color;
  327. } else
  328. {
  329. viewController.navigationController.navigationBar.tintColor = nil;
  330. }
  331. BOOL viewControllerBasedStatusBar = false;
  332. NSObject *viewControllerBasedStatusBarAppearance = [[NSBundle mainBundle] infoDictionary][@"UIViewControllerBasedStatusBarAppearance"];
  333. if (viewControllerBasedStatusBarAppearance && [viewControllerBasedStatusBarAppearance isKindOfClass:[NSNumber class]]) {
  334. viewControllerBasedStatusBar = [(NSNumber *)viewControllerBasedStatusBarAppearance boolValue];
  335. }
  336. NSString *statusBarTextColorSchemeSingleScreen = self.navigatorStyle[@"statusBarTextColorSchemeSingleScreen"];
  337. NSString *statusBarTextColorScheme = self.navigatorStyle[@"statusBarTextColorScheme"];
  338. NSString *finalColorScheme = statusBarTextColorSchemeSingleScreen ? : statusBarTextColorScheme;
  339. if (finalColorScheme && [finalColorScheme isEqualToString:@"light"]) {
  340. if (!statusBarTextColorSchemeSingleScreen) {
  341. viewController.navigationController.navigationBar.barStyle = UIBarStyleBlack;
  342. }
  343. self._statusBarTextColorSchemeLight = true;
  344. if (!viewControllerBasedStatusBarAppearance) {
  345. [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
  346. }
  347. [viewController setNeedsStatusBarAppearanceUpdate];
  348. } else {
  349. if (!statusBarTextColorSchemeSingleScreen) {
  350. viewController.navigationController.navigationBar.barStyle = UIBarStyleDefault;
  351. }
  352. self._statusBarTextColorSchemeLight = false;
  353. if (!viewControllerBasedStatusBarAppearance) {
  354. [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
  355. }
  356. [viewController setNeedsStatusBarAppearanceUpdate];
  357. }
  358. NSNumber *navBarHidden = self.navigatorStyle[@"navBarHidden"];
  359. BOOL navBarHiddenBool = navBarHidden ? [navBarHidden boolValue] : NO;
  360. if (viewController.navigationController.navigationBarHidden != navBarHiddenBool) {
  361. [viewController.navigationController setNavigationBarHidden:navBarHiddenBool animated:YES];
  362. }
  363. NSNumber *navBarHideOnScroll = self.navigatorStyle[@"navBarHideOnScroll"];
  364. BOOL navBarHideOnScrollBool = navBarHideOnScroll ? [navBarHideOnScroll boolValue] : NO;
  365. if (navBarHideOnScrollBool) {
  366. viewController.navigationController.hidesBarsOnSwipe = YES;
  367. } else {
  368. viewController.navigationController.hidesBarsOnSwipe = NO;
  369. }
  370. NSNumber *statusBarBlur = self.navigatorStyle[@"statusBarBlur"];
  371. BOOL statusBarBlurBool = statusBarBlur ? [statusBarBlur boolValue] : NO;
  372. if (statusBarBlurBool && ![viewController.view viewWithTag:BLUR_STATUS_TAG]) {
  373. UIVisualEffectView *blur = [[UIVisualEffectView alloc] initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleLight]];
  374. blur.frame = [[UIApplication sharedApplication] statusBarFrame];
  375. blur.tag = BLUR_STATUS_TAG;
  376. [viewController.view insertSubview:blur atIndex:0];
  377. }
  378. NSNumber *navBarBlur = self.navigatorStyle[@"navBarBlur"];
  379. BOOL navBarBlurBool = navBarBlur ? [navBarBlur boolValue] : NO;
  380. if (navBarBlurBool) {
  381. if (![viewController.navigationController.navigationBar viewWithTag:BLUR_NAVBAR_TAG]) {
  382. [self storeOriginalNavBarImages];
  383. [viewController.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
  384. viewController.navigationController.navigationBar.shadowImage = [UIImage new];
  385. UIVisualEffectView *blur = [[UIVisualEffectView alloc] initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleLight]];
  386. CGRect statusBarFrame = [[UIApplication sharedApplication] statusBarFrame];
  387. blur.frame = CGRectMake(0, -1 * statusBarFrame.size.height, viewController.navigationController.navigationBar.frame.size.width, viewController.navigationController.navigationBar.frame.size.height + statusBarFrame.size.height);
  388. blur.userInteractionEnabled = NO;
  389. blur.tag = BLUR_NAVBAR_TAG;
  390. [viewController.navigationController.navigationBar insertSubview:blur atIndex:0];
  391. [viewController.navigationController.navigationBar sendSubviewToBack:blur];
  392. }
  393. } else {
  394. UIView *blur = [viewController.navigationController.navigationBar viewWithTag:BLUR_NAVBAR_TAG];
  395. if (blur) {
  396. [blur removeFromSuperview];
  397. [viewController.navigationController.navigationBar setBackgroundImage:self.originalNavBarImages[@"bgImage"] forBarMetrics:UIBarMetricsDefault];
  398. viewController.navigationController.navigationBar.shadowImage = self.originalNavBarImages[@"shadowImage"];
  399. self.originalNavBarImages = nil;
  400. }
  401. }
  402. NSNumber *navBarTransparent = self.navigatorStyle[@"navBarTransparent"];
  403. BOOL navBarTransparentBool = navBarTransparent ? [navBarTransparent boolValue] : NO;
  404. void (^action)() = ^ {
  405. if (navBarTransparentBool)
  406. {
  407. if (![viewController.navigationController.navigationBar viewWithTag:TRANSPARENT_NAVBAR_TAG])
  408. {
  409. [self storeOriginalNavBarImages];
  410. [viewController.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
  411. viewController.navigationController.navigationBar.shadowImage = [UIImage new];
  412. UIView *transparentView = [[UIView alloc] initWithFrame:CGRectZero];
  413. transparentView.tag = TRANSPARENT_NAVBAR_TAG;
  414. [viewController.navigationController.navigationBar insertSubview:transparentView atIndex:0];
  415. }
  416. }
  417. else
  418. {
  419. UIView *transparentView = [viewController.navigationController.navigationBar viewWithTag:TRANSPARENT_NAVBAR_TAG];
  420. if (transparentView)
  421. {
  422. [transparentView removeFromSuperview];
  423. [viewController.navigationController.navigationBar setBackgroundImage:self.originalNavBarImages[@"bgImage"] forBarMetrics:UIBarMetricsDefault];
  424. viewController.navigationController.navigationBar.shadowImage = self.originalNavBarImages[@"shadowImage"];
  425. self.originalNavBarImages = nil;
  426. }
  427. }
  428. };
  429. if (!self.transitionCoordinator || self.transitionCoordinator.initiallyInteractive || !navBarTransparentBool || appeared) {
  430. action();
  431. } else {
  432. UIView* backgroundView = [self.navigationController.navigationBar valueForKey:@"backgroundView"];
  433. CGFloat originalAlpha = backgroundView.alpha;
  434. backgroundView.alpha = navBarTransparentBool ? 0.0 : 1.0;
  435. [self.transitionCoordinator animateAlongsideTransition:nil completion:^(id<UIViewControllerTransitionCoordinatorContext> _Nonnull context) {
  436. action();
  437. backgroundView.alpha = originalAlpha;
  438. }];
  439. }
  440. NSNumber *autoAdjustsScrollViewInsets = self.navigatorStyle[@"autoAdjustScrollViewInsets"];
  441. viewController.automaticallyAdjustsScrollViewInsets = autoAdjustsScrollViewInsets ? [autoAdjustsScrollViewInsets boolValue] : false;
  442. NSNumber *navBarTranslucent = self.navigatorStyle[@"navBarTranslucent"];
  443. BOOL navBarTranslucentBool = navBarTranslucent ? [navBarTranslucent boolValue] : NO;
  444. if (navBarTranslucentBool || navBarBlurBool) {
  445. viewController.navigationController.navigationBar.translucent = YES;
  446. } else {
  447. viewController.navigationController.navigationBar.translucent = NO;
  448. }
  449. NSNumber *extendedLayoutIncludesOpaqueBars = self.navigatorStyle[@"extendedLayoutIncludesOpaqueBars"];
  450. BOOL extendedLayoutIncludesOpaqueBarsBool = extendedLayoutIncludesOpaqueBars ? [extendedLayoutIncludesOpaqueBars boolValue] : NO;
  451. viewController.extendedLayoutIncludesOpaqueBars = extendedLayoutIncludesOpaqueBarsBool;
  452. NSNumber *drawUnderNavBar = self.navigatorStyle[@"drawUnderNavBar"];
  453. BOOL drawUnderNavBarBool = drawUnderNavBar ? [drawUnderNavBar boolValue] : NO;
  454. if (drawUnderNavBarBool) {
  455. viewController.edgesForExtendedLayout |= UIRectEdgeTop;
  456. }
  457. else {
  458. viewController.edgesForExtendedLayout &= ~UIRectEdgeTop;
  459. }
  460. NSNumber *drawUnderTabBar = self.navigatorStyle[@"drawUnderTabBar"];
  461. BOOL drawUnderTabBarBool = drawUnderTabBar ? [drawUnderTabBar boolValue] : NO;
  462. if (drawUnderTabBarBool) {
  463. viewController.edgesForExtendedLayout |= UIRectEdgeBottom;
  464. } else {
  465. viewController.edgesForExtendedLayout &= ~UIRectEdgeBottom;
  466. }
  467. NSNumber *removeNavBarBorder = self.navigatorStyle[@"navBarNoBorder"];
  468. BOOL removeNavBarBorderBool = removeNavBarBorder ? [removeNavBarBorder boolValue] : NO;
  469. if (removeNavBarBorderBool) {
  470. self.navBarHairlineImageView.hidden = YES;
  471. } else {
  472. self.navBarHairlineImageView.hidden = NO;
  473. }
  474. //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
  475. //overriding the delegate of the gesture prevents this from happening while keeping the gesture intact (another option was to disable it completely by demand)
  476. if(self.navigationController.viewControllers.count > 1){
  477. if (self.navigationController != nil && self.navigationController.interactivePopGestureRecognizer != nil)
  478. {
  479. id <UIGestureRecognizerDelegate> interactivePopGestureRecognizer = self.navigationController.interactivePopGestureRecognizer.delegate;
  480. if (interactivePopGestureRecognizer != nil && interactivePopGestureRecognizer != self)
  481. {
  482. self.originalInteractivePopGestureDelegate = interactivePopGestureRecognizer;
  483. self.navigationController.interactivePopGestureRecognizer.delegate = self;
  484. }
  485. }
  486. }
  487. NSString *navBarCustomView = self.navigatorStyle[@"navBarCustomView"];
  488. if (navBarCustomView && ![self.navigationItem.titleView isKindOfClass:[RCCCustomTitleView class]]) {
  489. if ([self.view isKindOfClass:[RCTRootView class]]) {
  490. RCTBridge *bridge = ((RCTRootView*)self.view).bridge;
  491. NSDictionary *initialProps = self.navigatorStyle[@"navBarCustomViewInitialProps"];
  492. RCTRootView *reactView = [[RCTRootView alloc] initWithBridge:bridge moduleName:navBarCustomView initialProperties:initialProps];
  493. RCCCustomTitleView *titleView = [[RCCCustomTitleView alloc] initWithFrame:self.navigationController.navigationBar.bounds subView:reactView alignment:self.navigatorStyle[@"navBarComponentAlignment"]];
  494. titleView.backgroundColor = [UIColor clearColor];
  495. reactView.backgroundColor = [UIColor clearColor];
  496. self.navigationItem.titleView = titleView;
  497. self.navigationItem.titleView.backgroundColor = [UIColor clearColor];
  498. self.navigationItem.titleView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
  499. self.navigationItem.titleView.clipsToBounds = YES;
  500. }
  501. }
  502. }
  503. -(void)storeOriginalNavBarImages {
  504. NSMutableDictionary *originalNavBarImages = [@{} mutableCopy];
  505. UIImage *bgImage = [self.navigationController.navigationBar backgroundImageForBarMetrics:UIBarMetricsDefault];
  506. if (bgImage != nil) {
  507. originalNavBarImages[@"bgImage"] = bgImage;
  508. }
  509. UIImage *shadowImage = self.navigationController.navigationBar.shadowImage;
  510. if (shadowImage != nil) {
  511. originalNavBarImages[@"shadowImage"] = shadowImage;
  512. }
  513. self.originalNavBarImages = originalNavBarImages;
  514. }
  515. -(void)setStyleOnDisappear {
  516. self.navBarHairlineImageView.hidden = NO;
  517. if (self.navigationController != nil && self.navigationController.interactivePopGestureRecognizer != nil && self.originalInteractivePopGestureDelegate != nil)
  518. {
  519. self.navigationController.interactivePopGestureRecognizer.delegate = self.originalInteractivePopGestureDelegate;
  520. self.originalInteractivePopGestureDelegate = nil;
  521. }
  522. }
  523. // only styles that can't be set on willAppear should be set here
  524. - (void)setStyleOnInit
  525. {
  526. NSNumber *tabBarHidden = self.navigatorStyle[@"tabBarHidden"];
  527. BOOL tabBarHiddenBool = tabBarHidden ? [tabBarHidden boolValue] : NO;
  528. if (tabBarHiddenBool) {
  529. self._hidesBottomBarWhenPushed = YES;
  530. } else {
  531. self._hidesBottomBarWhenPushed = NO;
  532. }
  533. NSNumber *statusBarHideWithNavBar = self.navigatorStyle[@"statusBarHideWithNavBar"];
  534. BOOL statusBarHideWithNavBarBool = statusBarHideWithNavBar ? [statusBarHideWithNavBar boolValue] : NO;
  535. if (statusBarHideWithNavBarBool) {
  536. self._statusBarHideWithNavBar = YES;
  537. } else {
  538. self._statusBarHideWithNavBar = NO;
  539. }
  540. NSNumber *statusBarHidden = self.navigatorStyle[@"statusBarHidden"];
  541. BOOL statusBarHiddenBool = statusBarHidden ? [statusBarHidden boolValue] : NO;
  542. if (statusBarHiddenBool) {
  543. self._statusBarHidden = YES;
  544. } else {
  545. self._statusBarHidden = NO;
  546. }
  547. }
  548. - (BOOL)hidesBottomBarWhenPushed
  549. {
  550. if (!self._hidesBottomBarWhenPushed) return NO;
  551. return (self.navigationController.topViewController == self);
  552. }
  553. - (BOOL)prefersStatusBarHidden
  554. {
  555. if (self._statusBarHidden) {
  556. return YES;
  557. }
  558. if (self._statusBarHideWithNavBar) {
  559. return self.navigationController.isNavigationBarHidden;
  560. } else {
  561. return NO;
  562. }
  563. }
  564. - (void)setNavBarVisibilityChange:(BOOL)animated {
  565. [self.navigationController setNavigationBarHidden:[self.navigatorStyle[@"navBarHidden"] boolValue] animated:animated];
  566. }
  567. - (UIStatusBarStyle)preferredStatusBarStyle
  568. {
  569. if (self._statusBarTextColorSchemeLight){
  570. return UIStatusBarStyleLightContent;
  571. } else {
  572. return UIStatusBarStyleDefault;
  573. }
  574. }
  575. - (UIImageView *)findHairlineImageViewUnder:(UIView *)view {
  576. if ([view isKindOfClass:UIImageView.class] && view.bounds.size.height <= 1.0) {
  577. return (UIImageView *)view;
  578. }
  579. for (UIView *subview in view.subviews) {
  580. UIImageView *imageView = [self findHairlineImageViewUnder:subview];
  581. if (imageView) {
  582. return imageView;
  583. }
  584. }
  585. return nil;
  586. }
  587. -(void)addExternalVCIfNecessary:(NSDictionary*)props
  588. {
  589. NSString *externalScreenClass = props[@"externalNativeScreenClass"];
  590. if (externalScreenClass != nil)
  591. {
  592. Class class = NSClassFromString(externalScreenClass);
  593. if (class != NULL)
  594. {
  595. id obj = [[class alloc] init];
  596. if (obj != nil && [obj isKindOfClass:[UIViewController class]] && [obj conformsToProtocol:@protocol(RCCExternalViewControllerProtocol)])
  597. {
  598. ((id <RCCExternalViewControllerProtocol>)obj).controllerDelegate = self;
  599. [obj setProps:props[@"externalNativeScreenProps"]];
  600. UIViewController *viewController = (UIViewController*)obj;
  601. [self addChildViewController:viewController];
  602. viewController.view.frame = self.view.bounds;
  603. [self.view addSubview:viewController.view];
  604. [viewController didMoveToParentViewController:self];
  605. }
  606. else
  607. {
  608. NSLog(@"addExternalVCIfNecessary: could not create instance. Make sure that your class is a UIViewController whihc confirms to RCCExternalViewControllerProtocol");
  609. }
  610. }
  611. else
  612. {
  613. NSLog(@"addExternalVCIfNecessary: could not create class from string. Check that the proper class name wass passed in ExternalNativeScreenClass");
  614. }
  615. }
  616. }
  617. #pragma mark - NewRelic
  618. - (NSString*) customNewRelicInteractionName
  619. {
  620. NSString *interactionName = nil;
  621. if (self.view != nil && [self.view isKindOfClass:[RCTRootView class]])
  622. {
  623. NSString *moduleName = ((RCTRootView*)self.view).moduleName;
  624. if(moduleName != nil)
  625. {
  626. interactionName = [NSString stringWithFormat:@"RCCViewController: %@", moduleName];
  627. }
  628. }
  629. if (interactionName == nil)
  630. {
  631. interactionName = [NSString stringWithFormat:@"RCCViewController with title: %@", self.title];
  632. }
  633. return interactionName;
  634. }
  635. #pragma mark - UIGestureRecognizerDelegate
  636. -(BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
  637. NSNumber *disabledBackGesture = self.navigatorStyle[@"disabledBackGesture"];
  638. BOOL disabledBackGestureBool = disabledBackGesture ? [disabledBackGesture boolValue] : NO;
  639. return !disabledBackGestureBool;
  640. }
  641. -(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
  642. NSNumber *disabledSimultaneousGesture = self.navigatorStyle[@"disabledSimultaneousGesture"];
  643. BOOL disabledSimultaneousGestureBool = disabledSimultaneousGesture ? [disabledSimultaneousGesture boolValue] : YES; // make default value of disabledSimultaneousGesture is true
  644. return !disabledSimultaneousGestureBool;
  645. }
  646. @end