react-native-navigation的迁移库

RNNRootViewController.m 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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)isAnimated {
  59. return self.options.animated ? [self.options.animated boolValue] : YES;
  60. }
  61. - (BOOL)prefersStatusBarHidden {
  62. if ([self.options.statusBarHidden boolValue]) {
  63. return YES;
  64. } else if ([self.options.statusBarHideWithTopBar boolValue]) {
  65. return self.navigationController.isNavigationBarHidden;
  66. }
  67. return NO;
  68. }
  69. - (UIInterfaceOrientationMask)supportedInterfaceOrientations {
  70. return self.options.supportedOrientations;
  71. }
  72. - (BOOL)hidesBottomBarWhenPushed
  73. {
  74. if (self.options.bottomTabs && self.options.bottomTabs.hidden) {
  75. return [self.options.bottomTabs.hidden boolValue];
  76. }
  77. return NO;
  78. }
  79. - (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated{
  80. RNNRootViewController* vc = (RNNRootViewController*)viewController;
  81. if (![vc.options.backButtonTransition isEqualToString:@"custom"]){
  82. navigationController.delegate = nil;
  83. }
  84. }
  85. - (id<UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController
  86. animationControllerForOperation:(UINavigationControllerOperation)operation
  87. fromViewController:(UIViewController*)fromVC
  88. toViewController:(UIViewController*)toVC {
  89. {
  90. if (operation == UINavigationControllerOperationPush) {
  91. return self.animator;
  92. } else if (operation == UINavigationControllerOperationPop) {
  93. return self.animator;
  94. } else {
  95. return nil;
  96. }
  97. }
  98. return nil;
  99. }
  100. -(void)applyTabBarItem {
  101. [self.options.bottomTab applyOn:self];
  102. }
  103. -(void)applyTopTabsOptions {
  104. [self.options.topTab applyOn:self];
  105. }
  106. /**
  107. * fix for #877, #878
  108. */
  109. -(void)onJsReload {
  110. [self cleanReactLeftovers];
  111. }
  112. /**
  113. * fix for #880
  114. */
  115. -(void)dealloc {
  116. [self cleanReactLeftovers];
  117. }
  118. -(void)cleanReactLeftovers {
  119. [[NSNotificationCenter defaultCenter] removeObserver:self];
  120. [[NSNotificationCenter defaultCenter] removeObserver:self.view];
  121. self.view = nil;
  122. }
  123. @end