react-native-navigation的迁移库

RNNTopBarOptions.m 6.9KB

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