react-native-navigation的迁移库

RNNNavigationController.m 3.8KB

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