react-native-navigation的迁移库

RNNRootViewController.m 4.6KB

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