react-native-navigation的迁移库

RNNTopBarOptions.m 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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.subtitle = self.subtitle;
  13. return self;
  14. }
  15. - (void)mergeWith:(NSDictionary *)otherOptions {
  16. [super mergeWith:otherOptions];
  17. self.title.subtitle = self.subtitle;
  18. }
  19. - (void)applyOn:(UIViewController*)viewController {
  20. [self.title applyOn:viewController];
  21. [self.background applyOn:viewController];
  22. if (@available(iOS 11.0, *)) {
  23. if (self.largeTitle){
  24. if ([self.largeTitle boolValue]) {
  25. viewController.navigationController.navigationBar.prefersLargeTitles = YES;
  26. viewController.navigationItem.largeTitleDisplayMode = UINavigationItemLargeTitleDisplayModeAlways;
  27. } else {
  28. viewController.navigationItem.largeTitleDisplayMode = UINavigationItemLargeTitleDisplayModeNever;
  29. }
  30. } else {
  31. viewController.navigationController.navigationBar.prefersLargeTitles = NO;
  32. viewController.navigationItem.largeTitleDisplayMode = UINavigationItemLargeTitleDisplayModeNever;
  33. }
  34. }
  35. if (self.visible) {
  36. [viewController.navigationController setNavigationBarHidden:![self.visible boolValue] animated:[self.animate boolValue]];
  37. } else {
  38. [viewController.navigationController setNavigationBarHidden:NO animated:NO];
  39. }
  40. if (self.hideOnScroll) {
  41. viewController.navigationController.hidesBarsOnSwipe = [self.hideOnScroll boolValue];
  42. } else {
  43. viewController.navigationController.hidesBarsOnSwipe = NO;
  44. }
  45. if (self.buttonColor) {
  46. UIColor* buttonColor = [RCTConvert UIColor:self.buttonColor];
  47. viewController.navigationController.navigationBar.tintColor = buttonColor;
  48. } else {
  49. viewController.navigationController.navigationBar.tintColor = nil;
  50. }
  51. if ([self.blur boolValue]) {
  52. if (![viewController.navigationController.navigationBar viewWithTag:BLUR_TOPBAR_TAG]) {
  53. [viewController.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
  54. viewController.navigationController.navigationBar.shadowImage = [UIImage new];
  55. UIVisualEffectView *blur = [[UIVisualEffectView alloc] initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleLight]];
  56. CGRect statusBarFrame = [[UIApplication sharedApplication] statusBarFrame];
  57. blur.frame = CGRectMake(0, -1 * statusBarFrame.size.height, viewController.navigationController.navigationBar.frame.size.width, viewController.navigationController.navigationBar.frame.size.height + statusBarFrame.size.height);
  58. blur.userInteractionEnabled = NO;
  59. blur.tag = BLUR_TOPBAR_TAG;
  60. [viewController.navigationController.navigationBar insertSubview:blur atIndex:0];
  61. [viewController.navigationController.navigationBar sendSubviewToBack:blur];
  62. }
  63. } else {
  64. UIView *blur = [viewController.navigationController.navigationBar viewWithTag:BLUR_TOPBAR_TAG];
  65. if (blur) {
  66. [viewController.navigationController.navigationBar setBackgroundImage: nil forBarMetrics:UIBarMetricsDefault];
  67. viewController.navigationController.navigationBar.shadowImage = nil;
  68. [blur removeFromSuperview];
  69. }
  70. }
  71. void (^disableTopBarTransparent)(void) = ^ {
  72. UIView *transparentView = [viewController.navigationController.navigationBar viewWithTag:TOP_BAR_TRANSPARENT_TAG];
  73. if (transparentView){
  74. [transparentView removeFromSuperview];
  75. [viewController.navigationController.navigationBar setBackgroundImage:self.originalTopBarImages[@"backgroundImage"] forBarMetrics:UIBarMetricsDefault];
  76. viewController.navigationController.navigationBar.shadowImage = self.originalTopBarImages[@"shadowImage"];
  77. self.originalTopBarImages = nil;
  78. }
  79. };
  80. if (self.transparent) {
  81. if ([self.transparent boolValue]) {
  82. if (![viewController.navigationController.navigationBar viewWithTag:TOP_BAR_TRANSPARENT_TAG]){
  83. [self storeOriginalTopBarImages:viewController];
  84. [viewController.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
  85. viewController.navigationController.navigationBar.shadowImage = [UIImage new];
  86. UIView *transparentView = [[UIView alloc] initWithFrame:CGRectZero];
  87. transparentView.tag = TOP_BAR_TRANSPARENT_TAG;
  88. [viewController.navigationController.navigationBar insertSubview:transparentView atIndex:0];
  89. }
  90. } else {
  91. disableTopBarTransparent();
  92. }
  93. } else {
  94. disableTopBarTransparent();
  95. }
  96. if (self.translucent) {
  97. viewController.navigationController.navigationBar.translucent = [self.translucent boolValue];
  98. } else {
  99. viewController.navigationController.navigationBar.translucent = NO;
  100. }
  101. if (self.drawBehind) {
  102. if ([self.drawBehind boolValue]) {
  103. viewController.edgesForExtendedLayout |= UIRectEdgeTop;
  104. } else {
  105. viewController.edgesForExtendedLayout &= ~UIRectEdgeTop;
  106. }
  107. } else {
  108. viewController.edgesForExtendedLayout = UIRectEdgeAll;
  109. }
  110. if (self.noBorder) {
  111. if ([self.noBorder boolValue]) {
  112. viewController.navigationController.navigationBar
  113. .shadowImage = [[UIImage alloc] init];
  114. } else {
  115. viewController.navigationController.navigationBar
  116. .shadowImage = nil;
  117. }
  118. }
  119. if (self.testID) {
  120. viewController.navigationController.navigationBar.accessibilityIdentifier = self.testID;
  121. }
  122. if (self.rightButtons || self.leftButtons) {
  123. _navigationButtons = [[RNNNavigationButtons alloc] initWithViewController:(RNNRootViewController*)viewController];
  124. [_navigationButtons applyLeftButtons:self.leftButtons rightButtons:self.rightButtons];
  125. }
  126. UIImage *image = self.backButtonImage ? [RCTConvert UIImage:self.backButtonImage] : nil;
  127. [[UINavigationBar appearance] setBackIndicatorImage:image];
  128. [[UINavigationBar appearance] setBackIndicatorTransitionMaskImage:image];
  129. if (self.hideBackButtonTitle) {
  130. self.backButtonTitle = @"";
  131. }
  132. if (self.backButtonTitle) {
  133. UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithTitle:self.backButtonTitle
  134. style:UIBarButtonItemStylePlain
  135. target:nil
  136. action:nil];
  137. viewController.navigationItem.backBarButtonItem = backItem;
  138. }
  139. viewController.navigationItem.hidesBackButton = [self.backButtonHidden boolValue];
  140. }
  141. -(void)storeOriginalTopBarImages:(UIViewController*)viewController {
  142. NSMutableDictionary *originalTopBarImages = [@{} mutableCopy];
  143. UIImage *bgImage = [viewController.navigationController.navigationBar backgroundImageForBarMetrics:UIBarMetricsDefault];
  144. if (bgImage != nil) {
  145. originalTopBarImages[@"backgroundImage"] = bgImage;
  146. }
  147. UIImage *shadowImage = viewController.navigationController.navigationBar.shadowImage;
  148. if (shadowImage != nil) {
  149. originalTopBarImages[@"shadowImage"] = shadowImage;
  150. }
  151. self.originalTopBarImages = originalTopBarImages;
  152. }
  153. @end