react-native-navigation的迁移库

RCCViewController.m 41KB

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