react-native-navigation的迁移库

RCCViewController.m 41KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894
  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) {
  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:nil completion:^(id<UIViewControllerTransitionCoordinatorContext> _Nonnull context) {
  424. action();
  425. backgroundView.alpha = originalAlpha;
  426. }];
  427. }
  428. NSNumber *autoAdjustsScrollViewInsets = self.navigatorStyle[@"autoAdjustScrollViewInsets"];
  429. viewController.automaticallyAdjustsScrollViewInsets = autoAdjustsScrollViewInsets ? [autoAdjustsScrollViewInsets boolValue] : false;
  430. NSNumber *navBarTranslucent = self.navigatorStyle[@"navBarTranslucent"];
  431. BOOL navBarTranslucentBool = navBarTranslucent ? [navBarTranslucent boolValue] : NO;
  432. if (navBarTranslucentBool || navBarBlurBool) {
  433. viewController.navigationController.navigationBar.translucent = YES;
  434. } else {
  435. viewController.navigationController.navigationBar.translucent = NO;
  436. }
  437. NSNumber *extendedLayoutIncludesOpaqueBars = self.navigatorStyle[@"extendedLayoutIncludesOpaqueBars"];
  438. BOOL extendedLayoutIncludesOpaqueBarsBool = extendedLayoutIncludesOpaqueBars ? [extendedLayoutIncludesOpaqueBars boolValue] : NO;
  439. viewController.extendedLayoutIncludesOpaqueBars = extendedLayoutIncludesOpaqueBarsBool;
  440. NSNumber *drawUnderNavBar = self.navigatorStyle[@"drawUnderNavBar"];
  441. BOOL drawUnderNavBarBool = drawUnderNavBar ? [drawUnderNavBar boolValue] : NO;
  442. if (drawUnderNavBarBool) {
  443. viewController.edgesForExtendedLayout |= UIRectEdgeTop;
  444. } else {
  445. viewController.edgesForExtendedLayout &= ~UIRectEdgeTop;
  446. }
  447. NSNumber *drawUnderTabBar = self.navigatorStyle[@"drawUnderTabBar"];
  448. BOOL drawUnderTabBarBool = drawUnderTabBar ? [drawUnderTabBar boolValue] : NO;
  449. if (drawUnderTabBarBool) {
  450. viewController.edgesForExtendedLayout |= UIRectEdgeBottom;
  451. } else {
  452. viewController.edgesForExtendedLayout &= ~UIRectEdgeBottom;
  453. }
  454. NSNumber *removeNavBarBorder = self.navigatorStyle[@"navBarNoBorder"];
  455. BOOL removeNavBarBorderBool = removeNavBarBorder ? [removeNavBarBorder boolValue] : NO;
  456. if (removeNavBarBorderBool) {
  457. self.navBarHairlineImageView.hidden = YES;
  458. } else {
  459. self.navBarHairlineImageView.hidden = NO;
  460. }
  461. //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
  462. //overriding the delegate of the gesture prevents this from happening while keeping the gesture intact (another option was to disable it completely by demand)
  463. if (self.navigationController.viewControllers.count > 1){
  464. if (self.navigationController != nil && self.navigationController.interactivePopGestureRecognizer != nil)
  465. {
  466. id <UIGestureRecognizerDelegate> interactivePopGestureRecognizer = self.navigationController.interactivePopGestureRecognizer.delegate;
  467. if (interactivePopGestureRecognizer != nil && interactivePopGestureRecognizer != self)
  468. {
  469. self.originalInteractivePopGestureDelegate = interactivePopGestureRecognizer;
  470. self.navigationController.interactivePopGestureRecognizer.delegate = self;
  471. }
  472. }
  473. }
  474. NSString *navBarCustomView = self.navigatorStyle[@"navBarCustomView"];
  475. if (navBarCustomView && ![self.navigationItem.titleView isKindOfClass:[RCCCustomTitleView class]]) {
  476. if ([self.view isKindOfClass:[RCTRootView class]]) {
  477. RCTBridge *bridge = ((RCTRootView*)self.view).bridge;
  478. NSDictionary *initialProps = self.navigatorStyle[@"navBarCustomViewInitialProps"];
  479. RCTRootView *reactView = [[RCTRootView alloc] initWithBridge:bridge moduleName:navBarCustomView initialProperties:initialProps];
  480. RCCCustomTitleView *titleView = [[RCCCustomTitleView alloc] initWithFrame:self.navigationController.navigationBar.bounds
  481. subView:reactView
  482. alignment:self.navigatorStyle[@"navBarComponentAlignment"]];
  483. self.navigationItem.titleView = titleView;
  484. self.navigationItem.titleView.backgroundColor = [UIColor clearColor];
  485. self.navigationItem.titleView.clipsToBounds = YES;
  486. }
  487. }
  488. // [self processTitleView:viewController
  489. // props:self.navigatorStyle
  490. // style:self.navigatorStyle];
  491. #if __IPHONE_OS_VERSION_MAX_ALLOWED > __IPHONE_10_3
  492. if (@available(iOS 11.0, *)) {
  493. if ([self.navigationController.navigationBar respondsToSelector:@selector(setPrefersLargeTitles:)]) {
  494. NSNumber *prefersLargeTitles = self.navigatorStyle[@"largeTitle"];
  495. if (prefersLargeTitles) {
  496. if ([prefersLargeTitles boolValue]) {
  497. self.navigationController.navigationBar.prefersLargeTitles = YES;
  498. self.navigationItem.largeTitleDisplayMode = UINavigationItemLargeTitleDisplayModeAlways;
  499. self.navigationItem.titleView = nil;
  500. } else {
  501. self.navigationItem.largeTitleDisplayMode = UINavigationItemLargeTitleDisplayModeNever;
  502. }
  503. } else {
  504. self.navigationController.navigationBar.prefersLargeTitles = NO;
  505. self.navigationItem.largeTitleDisplayMode = UINavigationItemLargeTitleDisplayModeNever;
  506. }
  507. }
  508. }
  509. #endif
  510. }
  511. -(void)processTitleView:(UIViewController*)viewController
  512. props:(NSDictionary*)props
  513. style:(NSDictionary*)style {
  514. BOOL isSetSubtitleBool = props[@"isSetSubtitle"] ? [props[@"isSetSubtitle"] boolValue] : NO;
  515. RCCTitleViewHelper *titleViewHelper = [[RCCTitleViewHelper alloc] init:viewController
  516. navigationController:self.navigationController
  517. title:props[@"title"]
  518. subtitle:props[@"subtitle"]
  519. titleImageData:props[@"titleImage"]
  520. isSetSubtitle:isSetSubtitleBool];
  521. [titleViewHelper setup:style];
  522. }
  523. - (void) viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {
  524. [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
  525. RCCCustomTitleView* customNavBar = (RCCCustomTitleView*) self.navigationItem.titleView;
  526. if (customNavBar && [customNavBar isKindOfClass:[RCCCustomTitleView class]]) {
  527. [customNavBar viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
  528. }
  529. }
  530. -(void)storeOriginalNavBarImages {
  531. NSMutableDictionary *originalNavBarImages = [@{} mutableCopy];
  532. UIImage *bgImage = [self.navigationController.navigationBar backgroundImageForBarMetrics:UIBarMetricsDefault];
  533. if (bgImage != nil) {
  534. originalNavBarImages[@"bgImage"] = bgImage;
  535. }
  536. UIImage *shadowImage = self.navigationController.navigationBar.shadowImage;
  537. if (shadowImage != nil) {
  538. originalNavBarImages[@"shadowImage"] = shadowImage;
  539. }
  540. self.originalNavBarImages = originalNavBarImages;
  541. }
  542. -(void)setStyleOnDisappear {
  543. self.navBarHairlineImageView.hidden = NO;
  544. if (self.navigationController != nil && self.navigationController.interactivePopGestureRecognizer != nil && self.originalInteractivePopGestureDelegate != nil)
  545. {
  546. self.navigationController.interactivePopGestureRecognizer.delegate = self.originalInteractivePopGestureDelegate;
  547. self.originalInteractivePopGestureDelegate = nil;
  548. }
  549. }
  550. // only styles that can't be set on willAppear should be set here
  551. - (void)setStyleOnInit {
  552. NSNumber *tabBarHidden = self.navigatorStyle[@"tabBarHidden"];
  553. BOOL tabBarHiddenBool = tabBarHidden ? [tabBarHidden boolValue] : NO;
  554. if (tabBarHiddenBool) {
  555. self._hidesBottomBarWhenPushed = YES;
  556. } else {
  557. self._hidesBottomBarWhenPushed = NO;
  558. }
  559. NSNumber *statusBarHideWithNavBar = self.navigatorStyle[@"statusBarHideWithNavBar"];
  560. BOOL statusBarHideWithNavBarBool = statusBarHideWithNavBar ? [statusBarHideWithNavBar boolValue] : NO;
  561. if (statusBarHideWithNavBarBool) {
  562. self._statusBarHideWithNavBar = YES;
  563. } else {
  564. self._statusBarHideWithNavBar = NO;
  565. }
  566. NSNumber *statusBarHidden = self.navigatorStyle[@"statusBarHidden"];
  567. BOOL statusBarHiddenBool = statusBarHidden ? [statusBarHidden boolValue] : NO;
  568. if (statusBarHiddenBool) {
  569. self._statusBarHidden = YES;
  570. } else {
  571. self._statusBarHidden = NO;
  572. }
  573. NSDictionary *preferredContentSize = self.navigatorStyle[@"preferredContentSize"];
  574. if (preferredContentSize) {
  575. NSNumber *width = preferredContentSize[@"width"];
  576. NSNumber *height = preferredContentSize[@"height"];
  577. if (width && height) {
  578. self.preferredContentSize = CGSizeMake([width floatValue], [height floatValue]);
  579. }
  580. }
  581. }
  582. - (BOOL)hidesBottomBarWhenPushed {
  583. if (!self._hidesBottomBarWhenPushed) return NO;
  584. return (self.navigationController.topViewController == self) && ![(RCCTabBarController*)self.tabBarController tabBarHidden];
  585. }
  586. - (BOOL)prefersStatusBarHidden {
  587. if (self._statusBarHidden) {
  588. return YES;
  589. }
  590. if (self._statusBarHideWithNavBar) {
  591. return self.navigationController.isNavigationBarHidden;
  592. } else {
  593. return NO;
  594. }
  595. }
  596. - (void)setNavBarVisibilityChange:(BOOL)animated {
  597. [self.navigationController setNavigationBarHidden:[self.navigatorStyle[@"navBarHidden"] boolValue] animated:animated];
  598. }
  599. - (UIStatusBarStyle)preferredStatusBarStyle {
  600. if (self._statusBarTextColorSchemeLight){
  601. return UIStatusBarStyleLightContent;
  602. } else {
  603. return UIStatusBarStyleDefault;
  604. }
  605. }
  606. - (UIImageView *)findHairlineImageViewUnder:(UIView *)view {
  607. if ([view isKindOfClass:UIImageView.class] && view.bounds.size.height <= 1.0) {
  608. return (UIImageView *)view;
  609. }
  610. for (UIView *subview in view.subviews) {
  611. UIImageView *imageView = [self findHairlineImageViewUnder:subview];
  612. if (imageView) {
  613. return imageView;
  614. }
  615. }
  616. return nil;
  617. }
  618. -(void)addExternalVCIfNecessary:(NSDictionary*)props {
  619. NSString *externalScreenClass = props[@"externalNativeScreenClass"];
  620. if (externalScreenClass != nil) {
  621. Class class = NSClassFromString(externalScreenClass);
  622. if (class != NULL) {
  623. id obj = [[class alloc] init];
  624. if (obj != nil && [obj isKindOfClass:[UIViewController class]] && [obj conformsToProtocol:@protocol(RCCExternalViewControllerProtocol)]) {
  625. ((id <RCCExternalViewControllerProtocol>)obj).controllerDelegate = self;
  626. [obj setProps:props[@"externalNativeScreenProps"]];
  627. UIViewController *viewController = (UIViewController*)obj;
  628. [self addChildViewController:viewController];
  629. viewController.view.frame = self.view.bounds;
  630. [self.view addSubview:viewController.view];
  631. [viewController didMoveToParentViewController:self];
  632. } else {
  633. NSLog(@"addExternalVCIfNecessary: could not create instance. Make sure that your class is a UIViewController whihc confirms to RCCExternalViewControllerProtocol");
  634. }
  635. } else {
  636. NSLog(@"addExternalVCIfNecessary: could not create class from string. Check that the proper class name wass passed in ExternalNativeScreenClass");
  637. }
  638. }
  639. }
  640. #pragma mark - Preview Actions
  641. - (void)onActionPress:(NSString *)id {
  642. if ([self.view isKindOfClass:[RCTRootView class]]) {
  643. RCTRootView *rootView = (RCTRootView *)self.view;
  644. if (rootView.appProperties && rootView.appProperties[@"navigatorEventID"]) {
  645. [[[RCCManager sharedInstance] getBridge].eventDispatcher
  646. sendAppEventWithName:rootView.appProperties[@"navigatorEventID"]
  647. body:@{
  648. @"type": @"PreviewActionPress",
  649. @"id": id
  650. }];
  651. }
  652. }
  653. }
  654. - (UIPreviewAction *) convertAction:(NSDictionary *)action {
  655. NSString *actionId = action[@"id"];
  656. NSString *actionTitle = action[@"title"];
  657. UIPreviewActionStyle actionStyle = UIPreviewActionStyleDefault;
  658. if ([action[@"style"] isEqualToString:@"selected"]) {
  659. actionStyle = UIPreviewActionStyleSelected;
  660. }
  661. if ([action[@"style"] isEqualToString:@"destructive"]) {
  662. actionStyle = UIPreviewActionStyleDestructive;
  663. }
  664. return [UIPreviewAction actionWithTitle:actionTitle style:actionStyle handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
  665. [self onActionPress:actionId];
  666. }];
  667. }
  668. #pragma mark - NewRelic
  669. - (NSString*) customNewRelicInteractionName {
  670. NSString *interactionName = nil;
  671. if (self.view != nil && [self.view isKindOfClass:[RCTRootView class]]) {
  672. NSString *moduleName = ((RCTRootView*)self.view).moduleName;
  673. if(moduleName != nil) {
  674. interactionName = [NSString stringWithFormat:@"RCCViewController: %@", moduleName];
  675. }
  676. }
  677. if (interactionName == nil) {
  678. interactionName = [NSString stringWithFormat:@"RCCViewController with title: %@", self.title];
  679. }
  680. return interactionName;
  681. }
  682. #pragma mark - UIGestureRecognizerDelegate
  683. -(BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
  684. NSNumber *disabledBackGesture = self.navigatorStyle[@"disabledBackGesture"];
  685. BOOL disabledBackGestureBool = disabledBackGesture ? [disabledBackGesture boolValue] : NO;
  686. return !disabledBackGestureBool;
  687. }
  688. -(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
  689. NSNumber *disabledSimultaneousGesture = self.navigatorStyle[@"disabledSimultaneousGesture"];
  690. BOOL disabledSimultaneousGestureBool = disabledSimultaneousGesture ? [disabledSimultaneousGesture boolValue] : YES; // make default value of disabledSimultaneousGesture is true
  691. return !disabledSimultaneousGestureBool;
  692. }
  693. #pragma mark - UIViewControllerPreviewingDelegate
  694. - (UIViewController *)previewingContext:(id<UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location {
  695. return self.previewController;
  696. }
  697. - (void)previewingContext:(id<UIViewControllerPreviewing>)previewingContext commitViewController:(UIViewController *)viewControllerToCommit {
  698. if (self.previewController.previewCommit == YES) {
  699. [self.previewController sendGlobalScreenEvent:@"willCommitPreview" endTimestampString:[self.previewController getTimestampString] shouldReset:YES];
  700. [self.previewController sendScreenChangedEvent:@"willCommitPreview"];
  701. [self.navigationController pushViewController:self.previewController animated:false];
  702. }
  703. }
  704. - (NSArray<id<UIPreviewActionItem>> *)previewActionItems {
  705. NSMutableArray *actions = [[NSMutableArray alloc] init];
  706. for (NSDictionary *previewAction in self.previewActions) {
  707. UIPreviewAction *action = [self convertAction:previewAction];
  708. NSDictionary *actionActions = previewAction[@"actions"];
  709. if (actionActions.count > 0) {
  710. NSMutableArray *group = [[NSMutableArray alloc] init];
  711. for (NSDictionary *previewGroupAction in actionActions) {
  712. [group addObject:[self convertAction:previewGroupAction]];
  713. }
  714. UIPreviewActionGroup *actionGroup = [UIPreviewActionGroup actionGroupWithTitle:action.title style:UIPreviewActionStyleDefault actions:group];
  715. [actions addObject:actionGroup];
  716. } else {
  717. [actions addObject:action];
  718. }
  719. }
  720. return actions;
  721. }
  722. @end