react-native-navigation的迁移库

RNNRootViewController.m 9.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. #import "RNNRootViewController.h"
  2. #import <React/RCTConvert.h>
  3. #import "RNNAnimator.h"
  4. #import "RNNCustomTitleView.h"
  5. #import "RNNPushAnimation.h"
  6. @interface RNNRootViewController() {
  7. UIView* _customTitleView;
  8. UIView* _customTopBar;
  9. UIView* _customTopBarBackground;
  10. }
  11. @property (nonatomic, strong) NSString* componentName;
  12. @property (nonatomic) BOOL _statusBarHidden;
  13. @property (nonatomic) BOOL isExternalComponent;
  14. @property (nonatomic) BOOL _optionsApplied;
  15. @property (nonatomic, copy) void (^rotationBlock)(void);
  16. @end
  17. @implementation RNNRootViewController
  18. -(instancetype)initWithName:(NSString*)name
  19. withOptions:(RNNNavigationOptions*)options
  20. withComponentId:(NSString*)componentId
  21. rootViewCreator:(id<RNNRootViewCreator>)creator
  22. eventEmitter:(RNNEventEmitter*)eventEmitter
  23. isExternalComponent:(BOOL)isExternalComponent {
  24. self = [super init];
  25. self.componentId = componentId;
  26. self.componentName = name;
  27. self.options = options;
  28. self.eventEmitter = eventEmitter;
  29. self.animator = [[RNNAnimator alloc] initWithTransitionOptions:self.options.customTransition];
  30. self.creator = creator;
  31. self.isExternalComponent = isExternalComponent;
  32. if (!self.isExternalComponent) {
  33. self.view = [creator createRootView:self.componentName rootViewId:self.componentId];
  34. }
  35. [[NSNotificationCenter defaultCenter] addObserver:self
  36. selector:@selector(onJsReload)
  37. name:RCTJavaScriptWillStartLoadingNotification
  38. object:nil];
  39. self.navigationController.delegate = self;
  40. [[NSNotificationCenter defaultCenter] addObserver:self
  41. selector:@selector(orientationDidChange:)
  42. name:UIDeviceOrientationDidChangeNotification
  43. object:nil];
  44. return self;
  45. }
  46. -(void)viewWillAppear:(BOOL)animated{
  47. [super viewWillAppear:animated];
  48. [self.options applyOn:self];
  49. [self optionsUpdated];
  50. }
  51. -(void)viewDidAppear:(BOOL)animated {
  52. [super viewDidAppear:animated];
  53. [self.eventEmitter sendComponentDidAppear:self.componentId componentName:self.componentName];
  54. }
  55. - (void)viewWillDisappear:(BOOL)animated {
  56. [super viewWillDisappear:animated];
  57. }
  58. -(void)viewDidDisappear:(BOOL)animated {
  59. [super viewDidDisappear:animated];
  60. [self.eventEmitter sendComponentDidDisappear:self.componentId componentName:self.componentName];
  61. }
  62. - (void)viewDidLoad {
  63. [super viewDidLoad];
  64. }
  65. - (void)optionsUpdated {
  66. [self setCustomNavigationTitleView];
  67. [self setCustomNavigationBarView];
  68. [self setCustomNavigationComponentBackground];
  69. }
  70. - (void)applyModalOptions {
  71. [self.options applyModalOptions:self];
  72. }
  73. - (void)mergeOptions:(NSDictionary *)options {
  74. [self.options mergeIfEmptyWith:options];
  75. }
  76. - (void)setCustomNavigationTitleView {
  77. if (!_customTitleView) {
  78. if (self.options.topBar.title.component.name) {
  79. RCTRootView *reactView = (RCTRootView*)[_creator createRootViewFromComponentOptions:self.options.topBar.title.component];
  80. _customTitleView = [[RNNCustomTitleView alloc] initWithFrame:self.navigationController.navigationBar.bounds subView:reactView alignment:self.options.topBar.title.component.alignment];
  81. reactView.backgroundColor = UIColor.clearColor;
  82. _customTitleView.backgroundColor = UIColor.clearColor;
  83. self.navigationItem.titleView = _customTitleView;
  84. } if ([self.navigationItem.title isKindOfClass:[RNNCustomTitleView class]] && !_customTitleView) {
  85. self.navigationItem.title = nil;
  86. }
  87. } else if (_customTitleView && _customTitleView.superview == nil) {
  88. if ([self.navigationItem.title isKindOfClass:[RNNCustomTitleView class]] && !_customTitleView) {
  89. self.navigationItem.title = nil;
  90. }
  91. self.navigationItem.titleView = _customTitleView;
  92. }
  93. }
  94. - (void)setCustomNavigationBarView {
  95. if (!_customTopBar) {
  96. if (self.options.topBar.component.name) {
  97. RCTRootView *reactView = (RCTRootView*)[_creator createRootViewFromComponentOptions:self.options.topBar.component];
  98. _customTopBar = [[RNNCustomTitleView alloc] initWithFrame:self.navigationController.navigationBar.bounds subView:reactView alignment:@"fill"];
  99. reactView.backgroundColor = UIColor.clearColor;
  100. _customTopBar.backgroundColor = UIColor.clearColor;
  101. [self.navigationController.navigationBar addSubview:_customTopBar];
  102. } else if ([[self.navigationController.navigationBar.subviews lastObject] isKindOfClass:[RNNCustomTitleView class]] && !_customTopBar) {
  103. [[self.navigationController.navigationBar.subviews lastObject] removeFromSuperview];
  104. }
  105. } else if (_customTopBar && _customTopBar.superview == nil) {
  106. if ([[self.navigationController.navigationBar.subviews lastObject] isKindOfClass:[RNNCustomTitleView class]] && !_customTopBar) {
  107. [[self.navigationController.navigationBar.subviews lastObject] removeFromSuperview];
  108. }
  109. [self.navigationController.navigationBar addSubview:_customTopBar];
  110. }
  111. }
  112. - (void)setCustomNavigationComponentBackground {
  113. if (!_customTopBarBackground) {
  114. if (self.options.topBar.background.component.name) {
  115. RCTRootView *reactView = (RCTRootView*)[_creator createRootViewFromComponentOptions:self.options.topBar.background.component];
  116. _customTopBarBackground = [[RNNCustomTitleView alloc] initWithFrame:self.navigationController.navigationBar.bounds subView:reactView alignment:@"fill"];
  117. [self.navigationController.navigationBar insertSubview:_customTopBarBackground atIndex:1];
  118. self.navigationController.navigationBar.clipsToBounds = YES;
  119. } else if ([[self.navigationController.navigationBar.subviews objectAtIndex:1] isKindOfClass:[RNNCustomTitleView class]]) {
  120. [[self.navigationController.navigationBar.subviews objectAtIndex:1] removeFromSuperview];
  121. self.navigationController.navigationBar.clipsToBounds = NO;
  122. }
  123. } if (_customTopBarBackground && _customTopBarBackground.superview == nil) {
  124. if ([[self.navigationController.navigationBar.subviews objectAtIndex:1] isKindOfClass:[RNNCustomTitleView class]]) {
  125. [[self.navigationController.navigationBar.subviews objectAtIndex:1] removeFromSuperview];
  126. }
  127. [self.navigationController.navigationBar insertSubview:_customTopBarBackground atIndex:1];
  128. self.navigationController.navigationBar.clipsToBounds = YES;
  129. }
  130. }
  131. -(BOOL)isCustomTransitioned {
  132. return self.options.customTransition.animations != nil;
  133. }
  134. - (BOOL)isCustomViewController {
  135. return self.isExternalComponent;
  136. }
  137. - (BOOL)prefersStatusBarHidden {
  138. if ([self.options.statusBar.hidden boolValue]) {
  139. return YES;
  140. } else if ([self.options.statusBar.hideWithTopBar boolValue]) {
  141. return self.navigationController.isNavigationBarHidden;
  142. }
  143. return NO;
  144. }
  145. - (UIStatusBarStyle)preferredStatusBarStyle {
  146. if (self.options.statusBar.style && [self.options.statusBar.style isEqualToString:@"light"]) {
  147. return UIStatusBarStyleLightContent;
  148. } else {
  149. return UIStatusBarStyleDefault;
  150. }
  151. }
  152. - (UIInterfaceOrientationMask)supportedInterfaceOrientations {
  153. return self.options.supportedOrientations;
  154. }
  155. - (BOOL)hidesBottomBarWhenPushed
  156. {
  157. if (self.options.bottomTabs && self.options.bottomTabs.visible) {
  158. return ![self.options.bottomTabs.visible boolValue];
  159. }
  160. return NO;
  161. }
  162. - (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated{
  163. RNNRootViewController* vc = (RNNRootViewController*)viewController;
  164. if (![vc.options.backButtonTransition isEqualToString:@"custom"]){
  165. navigationController.delegate = nil;
  166. }
  167. }
  168. - (id<UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController
  169. animationControllerForOperation:(UINavigationControllerOperation)operation
  170. fromViewController:(UIViewController*)fromVC
  171. toViewController:(UIViewController*)toVC {
  172. {
  173. if (self.animator) {
  174. return self.animator;
  175. } else if (operation == UINavigationControllerOperationPush && self.options.animations.push.hasCustomAnimation) {
  176. return [[RNNPushAnimation alloc] initWithScreenTransition:self.options.animations.push];
  177. } else if (operation == UINavigationControllerOperationPop && self.options.animations.pop.hasCustomAnimation) {
  178. return [[RNNPushAnimation alloc] initWithScreenTransition:self.options.animations.pop];
  179. } else {
  180. return nil;
  181. }
  182. }
  183. return nil;
  184. }
  185. - (nullable id <UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source {
  186. return [[RNNModalAnimation alloc] initWithScreenTransition:self.options.animations.showModal isDismiss:NO];
  187. }
  188. - (id<UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed {
  189. return [[RNNModalAnimation alloc] initWithScreenTransition:self.options.animations.dismissModal isDismiss:YES];
  190. }
  191. -(void)applyTabBarItem {
  192. [self.options.bottomTab applyOn:self];
  193. }
  194. -(void)applyTopTabsOptions {
  195. [self.options.topTab applyOn:self];
  196. }
  197. - (void)performOnRotation:(void (^)(void))block {
  198. _rotationBlock = block;
  199. }
  200. - (void)orientationDidChange:(NSNotification*)notification {
  201. if (_rotationBlock) {
  202. _rotationBlock();
  203. }
  204. }
  205. /**
  206. * fix for #877, #878
  207. */
  208. -(void)onJsReload {
  209. [self cleanReactLeftovers];
  210. }
  211. /**
  212. * fix for #880
  213. */
  214. -(void)dealloc {
  215. [self cleanReactLeftovers];
  216. }
  217. -(void)cleanReactLeftovers {
  218. [[NSNotificationCenter defaultCenter] removeObserver:self];
  219. [[NSNotificationCenter defaultCenter] removeObserver:self.view];
  220. self.view = nil;
  221. }
  222. @end