react-native-navigation的迁移库

RNNTopBarOptions.m 7.1KB

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