react-native-navigation的迁移库

RNNNavigationController.m 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #import "RNNNavigationController.h"
  2. #import "RNNRootViewController.h"
  3. #import "InteractivePopGestureDelegate.h"
  4. const NSInteger TOP_BAR_TRANSPARENT_TAG = 78264803;
  5. @interface RNNNavigationController()
  6. @property (nonatomic, strong) NSMutableDictionary* originalTopBarImages;
  7. @end
  8. @implementation RNNNavigationController
  9. - (void)viewDidLayoutSubviews {
  10. [super viewDidLayoutSubviews];
  11. [self.presenter applyOptionsOnViewDidLayoutSubviews:self.resolveOptions];
  12. }
  13. - (UIViewController *)getCurrentChild {
  14. return self.topViewController;
  15. }
  16. - (UIInterfaceOrientationMask)supportedInterfaceOrientations {
  17. return self.getCurrentChild.supportedInterfaceOrientations;
  18. }
  19. - (UINavigationController *)navigationController {
  20. return self;
  21. }
  22. - (UIStatusBarStyle)preferredStatusBarStyle {
  23. return self.getCurrentChild.preferredStatusBarStyle;
  24. }
  25. - (UIModalPresentationStyle)modalPresentationStyle {
  26. return self.getCurrentChild.modalPresentationStyle;
  27. }
  28. - (UIViewController *)popViewControllerAnimated:(BOOL)animated {
  29. if (self.viewControllers.count > 1) {
  30. UIViewController *controller = self.viewControllers[self.viewControllers.count - 2];
  31. if ([controller isKindOfClass:[RNNRootViewController class]]) {
  32. RNNRootViewController *rnnController = (RNNRootViewController *)controller;
  33. [self.presenter applyOptionsBeforePopping:rnnController.resolveOptions];
  34. }
  35. }
  36. return [super popViewControllerAnimated:animated];
  37. }
  38. - (UIViewController *)childViewControllerForStatusBarStyle {
  39. return self.topViewController;
  40. }
  41. - (void)setTopBarBackgroundColor:(UIColor *)backgroundColor {
  42. if (backgroundColor) {
  43. CGFloat bgColorAlpha = CGColorGetAlpha(backgroundColor.CGColor);
  44. if (bgColorAlpha == 0.0) {
  45. if (![self.navigationBar viewWithTag:TOP_BAR_TRANSPARENT_TAG]){
  46. [self storeOriginalTopBarImages:self];
  47. UIView *transparentView = [[UIView alloc] initWithFrame:CGRectZero];
  48. transparentView.backgroundColor = [UIColor clearColor];
  49. transparentView.tag = TOP_BAR_TRANSPARENT_TAG;
  50. [self.navigationBar insertSubview:transparentView atIndex:0];
  51. }
  52. self.navigationBar.translucent = YES;
  53. [self.navigationBar setBackgroundColor:[UIColor clearColor]];
  54. self.navigationBar.shadowImage = [UIImage new];
  55. [self.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
  56. } else {
  57. self.navigationBar.barTintColor = backgroundColor;
  58. UIView *transparentView = [self.navigationBar viewWithTag:TOP_BAR_TRANSPARENT_TAG];
  59. if (transparentView){
  60. [transparentView removeFromSuperview];
  61. [self.navigationBar setBackgroundImage:self.originalTopBarImages[@"backgroundImage"] forBarMetrics:UIBarMetricsDefault];
  62. self.navigationBar.shadowImage = self.originalTopBarImages[@"shadowImage"];
  63. self.originalTopBarImages = nil;
  64. }
  65. }
  66. } else {
  67. UIView *transparentView = [self.navigationBar viewWithTag:TOP_BAR_TRANSPARENT_TAG];
  68. if (transparentView){
  69. [transparentView removeFromSuperview];
  70. [self.navigationBar setBackgroundImage:self.originalTopBarImages[@"backgroundImage"] ? self.originalTopBarImages[@"backgroundImage"] : [self.navigationBar backgroundImageForBarMetrics:UIBarMetricsDefault] forBarMetrics:UIBarMetricsDefault];
  71. self.navigationBar.shadowImage = self.originalTopBarImages[@"shadowImage"] ? self.originalTopBarImages[@"shadowImage"] : self.navigationBar.shadowImage;
  72. self.originalTopBarImages = nil;
  73. }
  74. self.navigationBar.barTintColor = nil;
  75. }
  76. }
  77. - (void)storeOriginalTopBarImages:(UINavigationController *)navigationController {
  78. NSMutableDictionary *originalTopBarImages = [@{} mutableCopy];
  79. UIImage *bgImage = [navigationController.navigationBar backgroundImageForBarMetrics:UIBarMetricsDefault];
  80. if (bgImage != nil) {
  81. originalTopBarImages[@"backgroundImage"] = bgImage;
  82. }
  83. UIImage *shadowImage = navigationController.navigationBar.shadowImage;
  84. if (shadowImage != nil) {
  85. originalTopBarImages[@"shadowImage"] = shadowImage;
  86. }
  87. self.originalTopBarImages = originalTopBarImages;
  88. }
  89. @end