react-native-navigation的迁移库

RNNTopBarOptions.m 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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.largeTitle applyOn:viewController];
  22. [self.background applyOn:viewController];
  23. [self.backButton applyOn:viewController];
  24. if (@available(iOS 11.0, *)) {
  25. if ([self.searchBar boolValue] && !viewController.navigationItem.searchController) {
  26. UISearchController *search = [[UISearchController alloc]initWithSearchResultsController:nil];
  27. search.dimsBackgroundDuringPresentation = NO;
  28. if ([viewController conformsToProtocol:@protocol(UISearchResultsUpdating)]) {
  29. [search setSearchResultsUpdater:((UIViewController <UISearchResultsUpdating> *) viewController)];
  30. }
  31. if (self.searchBarPlaceholder) {
  32. search.searchBar.placeholder = self.searchBarPlaceholder;
  33. }
  34. viewController.navigationItem.searchController = search;
  35. // enable it back if needed on componentDidAppear
  36. viewController.navigationItem.hidesSearchBarWhenScrolling = NO;
  37. }
  38. }
  39. if (self.visible) {
  40. [viewController.navigationController setNavigationBarHidden:![self.visible boolValue] animated:[self.animate boolValue]];
  41. } else {
  42. [viewController.navigationController setNavigationBarHidden:NO animated:NO];
  43. }
  44. if (self.hideOnScroll) {
  45. viewController.navigationController.hidesBarsOnSwipe = [self.hideOnScroll boolValue];
  46. } else {
  47. viewController.navigationController.hidesBarsOnSwipe = NO;
  48. }
  49. if (self.buttonColor) {
  50. UIColor* buttonColor = [RCTConvert UIColor:self.buttonColor];
  51. viewController.navigationController.navigationBar.tintColor = buttonColor;
  52. } else {
  53. viewController.navigationController.navigationBar.tintColor = nil;
  54. }
  55. if ([self.blur boolValue]) {
  56. if (![viewController.navigationController.navigationBar viewWithTag:BLUR_TOPBAR_TAG]) {
  57. [viewController.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
  58. viewController.navigationController.navigationBar.shadowImage = [UIImage new];
  59. UIVisualEffectView *blur = [[UIVisualEffectView alloc] initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleLight]];
  60. CGRect statusBarFrame = [[UIApplication sharedApplication] statusBarFrame];
  61. blur.frame = CGRectMake(0, -1 * statusBarFrame.size.height, viewController.navigationController.navigationBar.frame.size.width, viewController.navigationController.navigationBar.frame.size.height + statusBarFrame.size.height);
  62. blur.userInteractionEnabled = NO;
  63. blur.tag = BLUR_TOPBAR_TAG;
  64. [viewController.navigationController.navigationBar insertSubview:blur atIndex:0];
  65. [viewController.navigationController.navigationBar sendSubviewToBack:blur];
  66. }
  67. } else {
  68. UIView *blur = [viewController.navigationController.navigationBar viewWithTag:BLUR_TOPBAR_TAG];
  69. if (blur) {
  70. [viewController.navigationController.navigationBar setBackgroundImage: nil forBarMetrics:UIBarMetricsDefault];
  71. viewController.navigationController.navigationBar.shadowImage = nil;
  72. [blur removeFromSuperview];
  73. }
  74. }
  75. void (^disableTopBarTransparent)(void) = ^ {
  76. UIView *transparentView = [viewController.navigationController.navigationBar viewWithTag:TOP_BAR_TRANSPARENT_TAG];
  77. if (transparentView){
  78. [transparentView removeFromSuperview];
  79. [viewController.navigationController.navigationBar setBackgroundImage:self.originalTopBarImages[@"backgroundImage"] forBarMetrics:UIBarMetricsDefault];
  80. viewController.navigationController.navigationBar.shadowImage = self.originalTopBarImages[@"shadowImage"];
  81. self.originalTopBarImages = nil;
  82. }
  83. };
  84. if (self.transparent) {
  85. if ([self.transparent boolValue]) {
  86. if (![viewController.navigationController.navigationBar viewWithTag:TOP_BAR_TRANSPARENT_TAG]){
  87. [self storeOriginalTopBarImages:viewController];
  88. [viewController.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
  89. viewController.navigationController.navigationBar.shadowImage = [UIImage new];
  90. UIView *transparentView = [[UIView alloc] initWithFrame:CGRectZero];
  91. transparentView.tag = TOP_BAR_TRANSPARENT_TAG;
  92. [viewController.navigationController.navigationBar insertSubview:transparentView atIndex:0];
  93. }
  94. } else {
  95. disableTopBarTransparent();
  96. }
  97. } else {
  98. disableTopBarTransparent();
  99. }
  100. if (self.translucent) {
  101. viewController.navigationController.navigationBar.translucent = [self.translucent boolValue];
  102. } else {
  103. viewController.navigationController.navigationBar.translucent = NO;
  104. }
  105. if (self.drawBehind) {
  106. if ([self.drawBehind boolValue]) {
  107. viewController.edgesForExtendedLayout |= UIRectEdgeTop;
  108. } else {
  109. viewController.edgesForExtendedLayout &= ~UIRectEdgeTop;
  110. }
  111. } else {
  112. viewController.edgesForExtendedLayout = UIRectEdgeAll;
  113. }
  114. if (self.noBorder) {
  115. if ([self.noBorder boolValue]) {
  116. viewController.navigationController.navigationBar
  117. .shadowImage = [[UIImage alloc] init];
  118. } else {
  119. viewController.navigationController.navigationBar
  120. .shadowImage = nil;
  121. }
  122. }
  123. if (self.testID) {
  124. viewController.navigationController.navigationBar.accessibilityIdentifier = self.testID;
  125. }
  126. if (self.rightButtons || self.leftButtons) {
  127. _navigationButtons = [[RNNNavigationButtons alloc] initWithViewController:(RNNRootViewController*)viewController];
  128. [_navigationButtons applyLeftButtons:self.leftButtons rightButtons:self.rightButtons];
  129. }
  130. }
  131. -(void)storeOriginalTopBarImages:(UIViewController*)viewController {
  132. NSMutableDictionary *originalTopBarImages = [@{} mutableCopy];
  133. UIImage *bgImage = [viewController.navigationController.navigationBar backgroundImageForBarMetrics:UIBarMetricsDefault];
  134. if (bgImage != nil) {
  135. originalTopBarImages[@"backgroundImage"] = bgImage;
  136. }
  137. UIImage *shadowImage = viewController.navigationController.navigationBar.shadowImage;
  138. if (shadowImage != nil) {
  139. originalTopBarImages[@"shadowImage"] = shadowImage;
  140. }
  141. self.originalTopBarImages = originalTopBarImages;
  142. }
  143. @end