react-native-navigation的迁移库

RNNTopBarOptions.m 6.9KB

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