react-native-navigation的迁移库

RNNRootViewController.m 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. #import "RNNRootViewController.h"
  2. #import <React/RCTConvert.h>
  3. #import "RNNAnimator.h"
  4. @interface RNNRootViewController()
  5. @property (nonatomic, strong) NSString* componentName;
  6. @property (nonatomic) BOOL _statusBarHidden;
  7. @end
  8. @implementation RNNRootViewController
  9. -(instancetype)initWithName:(NSString*)name
  10. withOptions:(RNNNavigationOptions*)options
  11. withComponentId:(NSString*)componentId
  12. rootViewCreator:(id<RNNRootViewCreator>)creator
  13. eventEmitter:(RNNEventEmitter*)eventEmitter
  14. animator:(RNNAnimator *)animator {
  15. self = [super init];
  16. self.componentId = componentId;
  17. self.componentName = name;
  18. self.options = options;
  19. self.eventEmitter = eventEmitter;
  20. self.animator = animator;
  21. self.view = [creator createRootView:self.componentName rootViewId:self.componentId];
  22. [[NSNotificationCenter defaultCenter] addObserver:self
  23. selector:@selector(onJsReload)
  24. name:RCTJavaScriptWillStartLoadingNotification
  25. object:nil];
  26. self.navigationController.modalPresentationStyle = UIModalPresentationCustom;
  27. self.navigationController.delegate = self;
  28. return self;
  29. }
  30. -(void)viewWillAppear:(BOOL)animated{
  31. [super viewWillAppear:animated];
  32. [self.options applyOn:self];
  33. [self sendLifecycleEvent:kDidMount];
  34. }
  35. -(void)viewDidAppear:(BOOL)animated {
  36. [super viewDidAppear:animated];
  37. [self.eventEmitter sendComponentDidAppear:self.componentId];
  38. [self sendLifecycleEvent:kDidAppear];
  39. }
  40. - (void)viewWillDisappear:(BOOL)animated {
  41. [super viewWillDisappear:animated];
  42. [self sendLifecycleEvent:kWillUnmount];
  43. }
  44. -(void)viewDidDisappear:(BOOL)animated {
  45. [super viewDidDisappear:animated];
  46. [self.eventEmitter sendComponentDidDisappear:self.componentId];
  47. [self sendLifecycleEvent:kDidDisappear];
  48. }
  49. - (void)viewDidLoad {
  50. [super viewDidLoad];
  51. }
  52. - (void)sendLifecycleEvent:(LifecycleEvent)event {
  53. [self.eventEmitter sendLifecycleEvent:[RNNComponentLifecycleEvent create:event componentName:self.componentName componentId:self.componentId]];
  54. }
  55. -(BOOL)isCustomTransitioned {
  56. return self.animator != nil;
  57. }
  58. - (BOOL)prefersStatusBarHidden {
  59. if ([self.options.statusBarHidden boolValue]) {
  60. return YES;
  61. } else if ([self.options.statusBarHideWithTopBar boolValue]) {
  62. return self.navigationController.isNavigationBarHidden;
  63. }
  64. return NO;
  65. }
  66. - (UIInterfaceOrientationMask)supportedInterfaceOrientations {
  67. return self.options.supportedOrientations;
  68. }
  69. - (BOOL)hidesBottomBarWhenPushed
  70. {
  71. if (self.options.bottomTabs && self.options.bottomTabs.hidden) {
  72. return [self.options.bottomTabs.hidden boolValue];
  73. }
  74. return NO;
  75. }
  76. - (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated{
  77. RNNRootViewController* vc = (RNNRootViewController*)viewController;
  78. if (![vc.options.backButtonTransition isEqualToString:@"custom"]){
  79. navigationController.delegate = nil;
  80. }
  81. }
  82. - (id<UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController
  83. animationControllerForOperation:(UINavigationControllerOperation)operation
  84. fromViewController:(UIViewController*)fromVC
  85. toViewController:(UIViewController*)toVC {
  86. {
  87. if (operation == UINavigationControllerOperationPush) {
  88. return self.animator;
  89. } else if (operation == UINavigationControllerOperationPop) {
  90. return self.animator;
  91. } else {
  92. return nil;
  93. }
  94. }
  95. return nil;
  96. }
  97. -(void)applyTabBarItem {
  98. [self.options.bottomTab applyOn:self];
  99. }
  100. -(void)applyTopTabsOptions {
  101. [self.options.topTab applyOn:self];
  102. }
  103. /**
  104. * fix for #877, #878
  105. */
  106. -(void)onJsReload {
  107. [self cleanReactLeftovers];
  108. }
  109. /**
  110. * fix for #880
  111. */
  112. -(void)dealloc {
  113. [self cleanReactLeftovers];
  114. }
  115. -(void)cleanReactLeftovers {
  116. [[NSNotificationCenter defaultCenter] removeObserver:self];
  117. [[NSNotificationCenter defaultCenter] removeObserver:self.view];
  118. self.view = nil;
  119. }
  120. @end