react-native-navigation的迁移库

RNNNavigationController.m 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. #import "RNNNavigationController.h"
  2. #import "RNNModalAnimation.h"
  3. #import "RNNRootViewController.h"
  4. const NSInteger TOP_BAR_TRANSPARENT_TAG = 78264803;
  5. @interface RNNNavigationController()
  6. @property (nonatomic, strong) NSMutableDictionary* originalTopBarImages;
  7. @end
  8. @implementation RNNNavigationController
  9. - (instancetype)initWithLayoutInfo:(RNNLayoutInfo *)layoutInfo childViewControllers:(NSArray *)childViewControllers options:(RNNNavigationOptions *)options defaultOptions:(RNNNavigationOptions *)defaultOptions presenter:(RNNNavigationControllerPresenter *)presenter {
  10. self = [super init];
  11. self.presenter = presenter;
  12. [self.presenter bindViewController:self];
  13. self.defaultOptions = defaultOptions;
  14. self.options = options;
  15. self.layoutInfo = layoutInfo;
  16. [self setViewControllers:childViewControllers];
  17. return self;
  18. }
  19. - (void)willMoveToParentViewController:(UIViewController *)parent {
  20. if (parent) {
  21. [_presenter applyOptionsOnWillMoveToParentViewController:self.resolveOptions];
  22. }
  23. }
  24. - (void)onChildWillAppear {
  25. [_presenter applyOptions:self.resolveOptions];
  26. [((UIViewController<RNNParentProtocol> *)self.parentViewController) onChildWillAppear];
  27. }
  28. - (RNNNavigationOptions *)resolveOptions {
  29. return [(RNNNavigationOptions *)[self.getCurrentChild.resolveOptions.copy mergeOptions:self.options] withDefault:self.defaultOptions];
  30. }
  31. - (void)mergeOptions:(RNNNavigationOptions *)options {
  32. [_presenter mergeOptions:options currentOptions:self.options defaultOptions:self.defaultOptions];
  33. [((UIViewController<RNNLayoutProtocol> *)self.parentViewController) mergeOptions:options];
  34. }
  35. - (void)overrideOptions:(RNNNavigationOptions *)options {
  36. [self.options overrideOptions:options];
  37. }
  38. - (UIInterfaceOrientationMask)supportedInterfaceOrientations {
  39. return self.getCurrentChild.supportedInterfaceOrientations;
  40. }
  41. - (UINavigationController *)navigationController {
  42. return self;
  43. }
  44. - (UIStatusBarStyle)preferredStatusBarStyle {
  45. return self.getCurrentChild.preferredStatusBarStyle;
  46. }
  47. - (UIModalPresentationStyle)modalPresentationStyle {
  48. return self.getCurrentChild.modalPresentationStyle;
  49. }
  50. - (UIViewController *)popViewControllerAnimated:(BOOL)animated {
  51. if (self.viewControllers.count > 1) {
  52. UIViewController *controller = self.viewControllers[self.viewControllers.count - 2];
  53. if ([controller isKindOfClass:[RNNRootViewController class]]) {
  54. RNNRootViewController *rnnController = (RNNRootViewController *)controller;
  55. [self.presenter applyOptionsBeforePopping:rnnController.resolveOptions];
  56. }
  57. }
  58. return [super popViewControllerAnimated:animated];
  59. }
  60. - (nullable id <UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source {
  61. return [[RNNModalAnimation alloc] initWithScreenTransition:self.getCurrentChild.resolveOptions.animations.showModal isDismiss:NO];
  62. }
  63. - (id<UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed {
  64. return [[RNNModalAnimation alloc] initWithScreenTransition:self.getCurrentChild.resolveOptions.animations.dismissModal isDismiss:YES];
  65. }
  66. - (UIViewController *)getCurrentChild {
  67. return ((UIViewController<RNNParentProtocol>*)self.topViewController);
  68. }
  69. - (UIViewController<RNNLeafProtocol> *)getCurrentLeaf {
  70. return [[self getCurrentChild] getCurrentLeaf];
  71. }
  72. - (UIViewController *)childViewControllerForStatusBarStyle {
  73. return self.topViewController;
  74. }
  75. - (void)setTopBarBackgroundColor:(UIColor *)backgroundColor {
  76. if (backgroundColor) {
  77. CGFloat bgColorAlpha = CGColorGetAlpha(backgroundColor.CGColor);
  78. if (bgColorAlpha == 0.0) {
  79. if (![self.navigationBar viewWithTag:TOP_BAR_TRANSPARENT_TAG]){
  80. [self storeOriginalTopBarImages:self];
  81. UIView *transparentView = [[UIView alloc] initWithFrame:CGRectZero];
  82. transparentView.backgroundColor = [UIColor clearColor];
  83. transparentView.tag = TOP_BAR_TRANSPARENT_TAG;
  84. [self.navigationBar insertSubview:transparentView atIndex:0];
  85. }
  86. self.navigationBar.translucent = YES;
  87. [self.navigationBar setBackgroundColor:[UIColor clearColor]];
  88. self.navigationBar.shadowImage = [UIImage new];
  89. [self.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
  90. } else {
  91. self.navigationBar.barTintColor = backgroundColor;
  92. UIView *transparentView = [self.navigationBar viewWithTag:TOP_BAR_TRANSPARENT_TAG];
  93. if (transparentView){
  94. [transparentView removeFromSuperview];
  95. [self.navigationBar setBackgroundImage:self.originalTopBarImages[@"backgroundImage"] forBarMetrics:UIBarMetricsDefault];
  96. self.navigationBar.shadowImage = self.originalTopBarImages[@"shadowImage"];
  97. self.originalTopBarImages = nil;
  98. }
  99. }
  100. } else {
  101. UIView *transparentView = [self.navigationBar viewWithTag:TOP_BAR_TRANSPARENT_TAG];
  102. if (transparentView){
  103. [transparentView removeFromSuperview];
  104. [self.navigationBar setBackgroundImage:self.originalTopBarImages[@"backgroundImage"] ? self.originalTopBarImages[@"backgroundImage"] : [self.navigationBar backgroundImageForBarMetrics:UIBarMetricsDefault] forBarMetrics:UIBarMetricsDefault];
  105. self.navigationBar.shadowImage = self.originalTopBarImages[@"shadowImage"] ? self.originalTopBarImages[@"shadowImage"] : self.navigationBar.shadowImage;
  106. self.originalTopBarImages = nil;
  107. }
  108. self.navigationBar.barTintColor = nil;
  109. }
  110. }
  111. - (void)storeOriginalTopBarImages:(UINavigationController *)navigationController {
  112. NSMutableDictionary *originalTopBarImages = [@{} mutableCopy];
  113. UIImage *bgImage = [navigationController.navigationBar backgroundImageForBarMetrics:UIBarMetricsDefault];
  114. if (bgImage != nil) {
  115. originalTopBarImages[@"backgroundImage"] = bgImage;
  116. }
  117. UIImage *shadowImage = navigationController.navigationBar.shadowImage;
  118. if (shadowImage != nil) {
  119. originalTopBarImages[@"shadowImage"] = shadowImage;
  120. }
  121. self.originalTopBarImages = originalTopBarImages;
  122. }
  123. @end