react-native-navigation的迁移库

RNNRootViewController.m 4.0KB

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