react-native-navigation的迁移库

RNNNavigationController.m 5.6KB

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