react-native-navigation的迁移库

RNNRootViewController.m 4.6KB

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