react-native-navigation的迁移库

RNNNavigationController.m 4.0KB

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