react-native-navigation的迁移库

RNNRootViewController.m 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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)mergeOptions:(NSDictionary *)options {
  52. [self.options mergeWith:options];
  53. }
  54. - (void)setCustomNavigationTitleView {
  55. if (self.options.topBar.customTitleViewName) {
  56. UIView *reactView = [_creator createRootView:self.options.topBar.customTitleViewName rootViewId:self.options.topBar.customTitleViewName];
  57. RNNCustomTitleView *titleView = [[RNNCustomTitleView alloc] initWithFrame:self.navigationController.navigationBar.bounds subView:reactView alignment:nil];
  58. self.navigationItem.titleView = titleView;
  59. }
  60. }
  61. - (void)setCustomNavigationBarView {
  62. if (self.options.topBar.customViewName) {
  63. UIView *reactView = [_creator createRootView:self.options.topBar.customViewName rootViewId:@"navBar"];
  64. RNNCustomTitleView *titleView = [[RNNCustomTitleView alloc] initWithFrame:self.navigationController.navigationBar.bounds subView:reactView alignment:nil];
  65. [self.navigationController.navigationBar addSubview:titleView];
  66. }
  67. }
  68. -(BOOL)isCustomTransitioned {
  69. return self.options.customTransition != nil;
  70. }
  71. - (BOOL)isAnimated {
  72. return self.options.animated ? [self.options.animated boolValue] : YES;
  73. }
  74. - (BOOL)prefersStatusBarHidden {
  75. if ([self.options.statusBarHidden boolValue]) {
  76. return YES;
  77. } else if ([self.options.statusBarHideWithTopBar boolValue]) {
  78. return self.navigationController.isNavigationBarHidden;
  79. }
  80. return NO;
  81. }
  82. - (UIInterfaceOrientationMask)supportedInterfaceOrientations {
  83. return self.options.supportedOrientations;
  84. }
  85. - (BOOL)hidesBottomBarWhenPushed
  86. {
  87. if (self.options.bottomTabs && self.options.bottomTabs.visible) {
  88. return ![self.options.bottomTabs.visible boolValue];
  89. }
  90. return NO;
  91. }
  92. - (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated{
  93. RNNRootViewController* vc = (RNNRootViewController*)viewController;
  94. if (![vc.options.backButtonTransition isEqualToString:@"custom"]){
  95. navigationController.delegate = nil;
  96. }
  97. }
  98. - (id<UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController
  99. animationControllerForOperation:(UINavigationControllerOperation)operation
  100. fromViewController:(UIViewController*)fromVC
  101. toViewController:(UIViewController*)toVC {
  102. {
  103. if (operation == UINavigationControllerOperationPush) {
  104. return self.animator;
  105. } else if (operation == UINavigationControllerOperationPop) {
  106. return self.animator;
  107. } else {
  108. return nil;
  109. }
  110. }
  111. return nil;
  112. }
  113. -(void)applyTabBarItem {
  114. [self.options.bottomTab applyOn:self];
  115. }
  116. -(void)applyTopTabsOptions {
  117. [self.options.topTab applyOn:self];
  118. }
  119. /**
  120. * fix for #877, #878
  121. */
  122. -(void)onJsReload {
  123. [self cleanReactLeftovers];
  124. }
  125. /**
  126. * fix for #880
  127. */
  128. -(void)dealloc {
  129. [self cleanReactLeftovers];
  130. }
  131. -(void)cleanReactLeftovers {
  132. [[NSNotificationCenter defaultCenter] removeObserver:self];
  133. [[NSNotificationCenter defaultCenter] removeObserver:self.view];
  134. self.view = nil;
  135. }
  136. @end