react-native-navigation的迁移库

RNNTopBarOptions.m 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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 alloc] initWithDict:dict[@"title"]];
  13. self.background = [[RNNBackgrounOptions alloc] initWithDict:dict[@"background"]];
  14. return self;
  15. }
  16. - (void)applyOn:(UIViewController*)viewController {
  17. [self.title applyOn:viewController];
  18. [self.background applyOn:viewController];
  19. if (@available(iOS 11.0, *)) {
  20. if (self.largeTitle){
  21. if ([self.largeTitle boolValue]) {
  22. viewController.navigationController.navigationBar.prefersLargeTitles = YES;
  23. viewController.navigationItem.largeTitleDisplayMode = UINavigationItemLargeTitleDisplayModeAlways;
  24. } else {
  25. viewController.navigationItem.largeTitleDisplayMode = UINavigationItemLargeTitleDisplayModeNever;
  26. }
  27. } else {
  28. viewController.navigationController.navigationBar.prefersLargeTitles = NO;
  29. viewController.navigationItem.largeTitleDisplayMode = UINavigationItemLargeTitleDisplayModeNever;
  30. }
  31. }
  32. if (self.visible) {
  33. [viewController.navigationController setNavigationBarHidden:![self.visible boolValue] animated:[self.animate boolValue]];
  34. } else {
  35. [viewController.navigationController setNavigationBarHidden:NO animated:NO];
  36. }
  37. if (self.hideOnScroll) {
  38. viewController.navigationController.hidesBarsOnSwipe = [self.hideOnScroll boolValue];
  39. } else {
  40. viewController.navigationController.hidesBarsOnSwipe = NO;
  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. } else {
  96. viewController.navigationController.navigationBar.translucent = NO;
  97. }
  98. if (self.drawBehind) {
  99. if ([self.drawBehind boolValue]) {
  100. viewController.edgesForExtendedLayout |= UIRectEdgeTop;
  101. } else {
  102. viewController.edgesForExtendedLayout &= ~UIRectEdgeTop;
  103. }
  104. } else {
  105. viewController.edgesForExtendedLayout = UIRectEdgeAll;
  106. }
  107. if (self.noBorder) {
  108. if ([self.noBorder boolValue]) {
  109. viewController.navigationController.navigationBar
  110. .shadowImage = [[UIImage alloc] init];
  111. } else {
  112. viewController.navigationController.navigationBar
  113. .shadowImage = nil;
  114. }
  115. }
  116. if (self.testID) {
  117. viewController.navigationController.navigationBar.accessibilityIdentifier = self.testID;
  118. }
  119. if (self.rightButtons || self.leftButtons) {
  120. _navigationButtons = [[RNNNavigationButtons alloc] initWithViewController:(RNNRootViewController*)viewController];
  121. [_navigationButtons applyLeftButtons:self.leftButtons rightButtons:self.rightButtons];
  122. }
  123. UIImage *image = self.backButtonImage ? [RCTConvert UIImage:self.backButtonImage] : nil;
  124. [[UINavigationBar appearance] setBackIndicatorImage:image];
  125. [[UINavigationBar appearance] setBackIndicatorTransitionMaskImage:image];
  126. if (self.hideBackButtonTitle) {
  127. self.backButtonTitle = @"";
  128. }
  129. if (self.backButtonTitle) {
  130. UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithTitle:self.backButtonTitle
  131. style:UIBarButtonItemStylePlain
  132. target:nil
  133. action:nil];
  134. viewController.navigationItem.backBarButtonItem = backItem;
  135. }
  136. viewController.navigationItem.hidesBackButton = [self.backButtonHidden boolValue];
  137. }
  138. -(void)storeOriginalTopBarImages:(UIViewController*)viewController {
  139. NSMutableDictionary *originalTopBarImages = [@{} mutableCopy];
  140. UIImage *bgImage = [viewController.navigationController.navigationBar backgroundImageForBarMetrics:UIBarMetricsDefault];
  141. if (bgImage != nil) {
  142. originalTopBarImages[@"backgroundImage"] = bgImage;
  143. }
  144. UIImage *shadowImage = viewController.navigationController.navigationBar.shadowImage;
  145. if (shadowImage != nil) {
  146. originalTopBarImages[@"shadowImage"] = shadowImage;
  147. }
  148. self.originalTopBarImages = originalTopBarImages;
  149. }
  150. @end