react-native-navigation的迁移库

RNNTopBarOptions.m 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. #import "RNNTopBarOptions.h"
  2. #import "RNNNavigationButtons.h"
  3. #import "RNNCustomTitleView.h"
  4. extern const NSInteger BLUR_TOPBAR_TAG;
  5. @interface RNNTopBarOptions ()
  6. @property (nonatomic, strong) NSMutableDictionary* originalTopBarImages;
  7. @property (nonatomic, strong) RNNNavigationButtons* navigationButtons;
  8. @end
  9. @implementation RNNTopBarOptions
  10. - (instancetype)initWithDict:(NSDictionary *)dict {
  11. self = [super initWithDict:dict];
  12. self.title = [RNNTitleOptions new];
  13. return self;
  14. }
  15. - (void)applyOn:(UIViewController*)viewController {
  16. [self.title applyOn:viewController];
  17. if (self.backgroundColor) {
  18. UIColor* backgroundColor = [RCTConvert UIColor:self.backgroundColor];
  19. viewController.navigationController.navigationBar.barTintColor = backgroundColor;
  20. } else {
  21. viewController.navigationController.navigationBar.barTintColor = nil;
  22. }
  23. if (@available(iOS 11.0, *)) {
  24. if (self.largeTitle){
  25. if ([self.largeTitle boolValue]) {
  26. viewController.navigationController.navigationBar.prefersLargeTitles = YES;
  27. viewController.navigationItem.largeTitleDisplayMode = UINavigationItemLargeTitleDisplayModeAlways;
  28. } else {
  29. viewController.navigationItem.largeTitleDisplayMode = UINavigationItemLargeTitleDisplayModeNever;
  30. }
  31. } else {
  32. viewController.navigationController.navigationBar.prefersLargeTitles = NO;
  33. viewController.navigationItem.largeTitleDisplayMode = UINavigationItemLargeTitleDisplayModeNever;
  34. }
  35. }
  36. if (self.visible) {
  37. [viewController.navigationController setNavigationBarHidden:![self.visible boolValue] animated:[self.animate boolValue]];
  38. }
  39. if (self.hideOnScroll) {
  40. viewController.navigationController.hidesBarsOnSwipe = [self.hideOnScroll boolValue];
  41. }
  42. if (self.buttonColor) {
  43. UIColor* buttonColor = [RCTConvert UIColor:self.buttonColor];
  44. viewController.navigationController.navigationBar.tintColor = buttonColor;
  45. } else {
  46. viewController.navigationController.navigationBar.tintColor = nil;
  47. }
  48. if ([self.blur boolValue]) {
  49. if (![viewController.navigationController.navigationBar viewWithTag:BLUR_TOPBAR_TAG]) {
  50. [viewController.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
  51. viewController.navigationController.navigationBar.shadowImage = [UIImage new];
  52. UIVisualEffectView *blur = [[UIVisualEffectView alloc] initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleLight]];
  53. CGRect statusBarFrame = [[UIApplication sharedApplication] statusBarFrame];
  54. blur.frame = CGRectMake(0, -1 * statusBarFrame.size.height, viewController.navigationController.navigationBar.frame.size.width, viewController.navigationController.navigationBar.frame.size.height + statusBarFrame.size.height);
  55. blur.userInteractionEnabled = NO;
  56. blur.tag = BLUR_TOPBAR_TAG;
  57. [viewController.navigationController.navigationBar insertSubview:blur atIndex:0];
  58. [viewController.navigationController.navigationBar sendSubviewToBack:blur];
  59. }
  60. } else {
  61. UIView *blur = [viewController.navigationController.navigationBar viewWithTag:BLUR_TOPBAR_TAG];
  62. if (blur) {
  63. [viewController.navigationController.navigationBar setBackgroundImage: nil forBarMetrics:UIBarMetricsDefault];
  64. viewController.navigationController.navigationBar.shadowImage = nil;
  65. [blur removeFromSuperview];
  66. }
  67. }
  68. void (^disableTopBarTransparent)(void) = ^ {
  69. UIView *transparentView = [viewController.navigationController.navigationBar viewWithTag:TOP_BAR_TRANSPARENT_TAG];
  70. if (transparentView){
  71. [transparentView removeFromSuperview];
  72. [viewController.navigationController.navigationBar setBackgroundImage:self.originalTopBarImages[@"backgroundImage"] forBarMetrics:UIBarMetricsDefault];
  73. viewController.navigationController.navigationBar.shadowImage = self.originalTopBarImages[@"shadowImage"];
  74. self.originalTopBarImages = nil;
  75. }
  76. };
  77. if (self.transparent) {
  78. if ([self.transparent boolValue]) {
  79. if (![viewController.navigationController.navigationBar viewWithTag:TOP_BAR_TRANSPARENT_TAG]){
  80. [self storeOriginalTopBarImages:viewController];
  81. [viewController.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
  82. viewController.navigationController.navigationBar.shadowImage = [UIImage new];
  83. UIView *transparentView = [[UIView alloc] initWithFrame:CGRectZero];
  84. transparentView.tag = TOP_BAR_TRANSPARENT_TAG;
  85. [viewController.navigationController.navigationBar insertSubview:transparentView atIndex:0];
  86. }
  87. } else {
  88. disableTopBarTransparent();
  89. }
  90. } else {
  91. disableTopBarTransparent();
  92. }
  93. if (self.translucent) {
  94. viewController.navigationController.navigationBar.translucent = [self.translucent boolValue];
  95. }
  96. if (self.drawBehind) {
  97. if ([self.drawBehind boolValue]) {
  98. viewController.edgesForExtendedLayout |= UIRectEdgeTop;
  99. } else {
  100. viewController.edgesForExtendedLayout &= ~UIRectEdgeTop;
  101. }
  102. }
  103. if (self.noBorder) {
  104. if ([self.noBorder boolValue]) {
  105. viewController.navigationController.navigationBar
  106. .shadowImage = [[UIImage alloc] init];
  107. } else {
  108. viewController.navigationController.navigationBar
  109. .shadowImage = nil;
  110. }
  111. }
  112. if (self.testID) {
  113. viewController.navigationController.navigationBar.accessibilityIdentifier = self.testID;
  114. }
  115. if (self.rightButtons || self.leftButtons) {
  116. _navigationButtons = [[RNNNavigationButtons alloc] initWithViewController:(RNNRootViewController*)viewController];
  117. [_navigationButtons applyLeftButtons:self.leftButtons rightButtons:self.rightButtons];
  118. }
  119. }
  120. -(void)storeOriginalTopBarImages:(UIViewController*)viewController {
  121. NSMutableDictionary *originalTopBarImages = [@{} mutableCopy];
  122. UIImage *bgImage = [viewController.navigationController.navigationBar backgroundImageForBarMetrics:UIBarMetricsDefault];
  123. if (bgImage != nil) {
  124. originalTopBarImages[@"backgroundImage"] = bgImage;
  125. }
  126. UIImage *shadowImage = viewController.navigationController.navigationBar.shadowImage;
  127. if (shadowImage != nil) {
  128. originalTopBarImages[@"shadowImage"] = shadowImage;
  129. }
  130. self.originalTopBarImages = originalTopBarImages;
  131. }
  132. @end