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