react-native-navigation的迁移库

RNNRootViewController.m 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #import "RNNRootViewController.h"
  2. #import <React/RCTConvert.h>
  3. #import "RNNAnimator.h"
  4. #import "RNNNavigationButtons.h"
  5. @interface RNNRootViewController()
  6. @property (nonatomic, strong) NSString* containerName;
  7. @property (nonatomic) BOOL _statusBarHidden;
  8. @property (nonatomic, strong) RNNNavigationButtons* navigationButtons;
  9. @end
  10. @implementation RNNRootViewController
  11. -(instancetype)initWithName:(NSString*)name
  12. withOptions:(RNNNavigationOptions*)options
  13. withContainerId:(NSString*)containerId
  14. rootViewCreator:(id<RNNRootViewCreator>)creator
  15. eventEmitter:(RNNEventEmitter*)eventEmitter {
  16. self = [super init];
  17. self.containerId = containerId;
  18. self.containerName = name;
  19. self.navigationOptions = options;
  20. self.eventEmitter = eventEmitter;
  21. self.view = [creator createRootView:self.containerName rootViewId:self.containerId];
  22. [[NSNotificationCenter defaultCenter] addObserver:self
  23. selector:@selector(onJsReload)
  24. name:RCTJavaScriptWillStartLoadingNotification
  25. object:nil];
  26. self.animator = [[RNNAnimator alloc] init];
  27. self.navigationController.modalPresentationStyle = UIModalPresentationCustom;
  28. self.navigationController.delegate = self;
  29. self.navigationButtons = [[RNNNavigationButtons alloc] initWithViewController:self];
  30. return self;
  31. }
  32. -(void)viewWillAppear:(BOOL)animated{
  33. [super viewWillAppear:animated];
  34. [self.navigationOptions applyOn:self];
  35. [self applyNavigationButtons];
  36. }
  37. - (void)viewDidLoad {
  38. [super viewDidLoad];
  39. }
  40. - (BOOL)prefersStatusBarHidden {
  41. if ([self.navigationOptions.statusBarHidden boolValue]) {
  42. return YES;
  43. } else if ([self.navigationOptions.statusBarHideWithTopBar boolValue]) {
  44. return self.navigationController.isNavigationBarHidden;
  45. }
  46. return NO;
  47. }
  48. - (UIInterfaceOrientationMask)supportedInterfaceOrientations {
  49. return self.navigationOptions.supportedOrientations;
  50. }
  51. - (BOOL)hidesBottomBarWhenPushed
  52. {
  53. if (self.navigationOptions.bottomTabs && self.navigationOptions.bottomTabs.hidden) {
  54. return [self.navigationOptions.bottomTabs.hidden boolValue];
  55. }
  56. return NO;
  57. }
  58. -(void)viewDidAppear:(BOOL)animated {
  59. [super viewDidAppear:animated];
  60. [self.eventEmitter sendContainerDidAppear:self.containerId];
  61. }
  62. -(void)viewDidDisappear:(BOOL)animated {
  63. [super viewDidDisappear:animated];
  64. [self.eventEmitter sendContainerDidDisappear:self.containerId];
  65. }
  66. - (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated{
  67. RNNRootViewController* vc = (RNNRootViewController*)viewController;
  68. if (![vc.navigationOptions.backButtonTransition isEqualToString:@"custom"]){
  69. navigationController.delegate = nil;
  70. }
  71. }
  72. - (id<UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController
  73. animationControllerForOperation:(UINavigationControllerOperation)operation
  74. fromViewController:(UIViewController*)fromVC
  75. toViewController:(UIViewController*)toVC {
  76. {
  77. if (operation == UINavigationControllerOperationPush) {
  78. return self.animator;
  79. } else if (operation == UINavigationControllerOperationPop) {
  80. return self.animator;
  81. } else {
  82. return nil;
  83. }
  84. }
  85. return nil;
  86. }
  87. -(void) applyNavigationButtons{
  88. [self.navigationButtons applyLeftButtons:self.navigationOptions.leftButtons rightButtons:self.navigationOptions.rightButtons];
  89. }
  90. /**
  91. * fix for #877, #878
  92. */
  93. -(void)onJsReload {
  94. [self cleanReactLeftovers];
  95. }
  96. /**
  97. * fix for #880
  98. */
  99. -(void)dealloc {
  100. [self cleanReactLeftovers];
  101. }
  102. -(void)cleanReactLeftovers {
  103. [[NSNotificationCenter defaultCenter] removeObserver:self];
  104. [[NSNotificationCenter defaultCenter] removeObserver:self.view];
  105. self.view = nil;
  106. }
  107. @end