react-native-navigation的迁移库

RNNTopBarOptions.m 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. search.searchBar.delegate = (id<UISearchBarDelegate>)viewController;
  32. if (self.searchBarPlaceholder) {
  33. search.searchBar.placeholder = self.searchBarPlaceholder;
  34. }
  35. viewController.navigationItem.searchController = search;
  36. viewController.navigationItem.hidesSearchBarWhenScrolling = [self.searchBarHiddenWhenScrolling boolValue];
  37. // Fixes #3450, otherwise, UIKit will infer the presentation context to be the root most view controller
  38. viewController.definesPresentationContext = YES;
  39. }
  40. }
  41. if (self.visible) {
  42. [viewController.navigationController setNavigationBarHidden:![self.visible boolValue] animated:[self.animate boolValue]];
  43. } else {
  44. [viewController.navigationController setNavigationBarHidden:NO animated:NO];
  45. }
  46. if (self.hideOnScroll) {
  47. viewController.navigationController.hidesBarsOnSwipe = [self.hideOnScroll boolValue];
  48. } else {
  49. viewController.navigationController.hidesBarsOnSwipe = NO;
  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.barStyle) {
  97. viewController.navigationController.navigationBar.barStyle = [RCTConvert UIBarStyle:self.barStyle];
  98. } else {
  99. viewController.navigationController.navigationBar.barStyle = UIBarStyleDefault;
  100. }
  101. if (self.translucent) {
  102. viewController.navigationController.navigationBar.translucent = [self.translucent boolValue];
  103. } else {
  104. viewController.navigationController.navigationBar.translucent = NO;
  105. }
  106. if (self.drawBehind) {
  107. if ([self.drawBehind boolValue]) {
  108. viewController.edgesForExtendedLayout |= UIRectEdgeTop;
  109. } else {
  110. viewController.edgesForExtendedLayout &= ~UIRectEdgeTop;
  111. }
  112. } else {
  113. viewController.edgesForExtendedLayout = UIRectEdgeAll;
  114. }
  115. if (self.noBorder) {
  116. if ([self.noBorder boolValue]) {
  117. viewController.navigationController.navigationBar
  118. .shadowImage = [[UIImage alloc] init];
  119. } else {
  120. viewController.navigationController.navigationBar
  121. .shadowImage = nil;
  122. }
  123. }
  124. if (self.testID) {
  125. viewController.navigationController.navigationBar.accessibilityIdentifier = self.testID;
  126. }
  127. if (self.rightButtons || self.leftButtons) {
  128. _navigationButtons = [[RNNNavigationButtons alloc] initWithViewController:(RNNRootViewController*)viewController];
  129. [_navigationButtons applyLeftButtons:self.leftButtons rightButtons:self.rightButtons defaultLeftButtonStyle:self.leftButtonStyle defaultRightButtonStyle:self.rightButtonStyle];
  130. }
  131. }
  132. - (void)setLeftButtons:(id)leftButtons {
  133. if ([leftButtons isKindOfClass:[NSArray class]]) {
  134. _leftButtons = leftButtons;
  135. } else if ([leftButtons isKindOfClass:[NSDictionary class]]) {
  136. if (leftButtons[@"id"]) {
  137. _leftButtons = @[leftButtons];
  138. } else {
  139. [_leftButtonStyle mergeWith:leftButtons];
  140. }
  141. }
  142. }
  143. - (void)setRightButtons:(id)rightButtons {
  144. if ([rightButtons isKindOfClass:[NSArray class]]) {
  145. _rightButtons = rightButtons;
  146. } else if ([rightButtons isKindOfClass:[NSDictionary class]]) {
  147. if (rightButtons[@"id"]) {
  148. _rightButtons = @[rightButtons];
  149. } else {
  150. [_rightButtonStyle mergeWith:rightButtons];
  151. }
  152. }
  153. }
  154. -(void)storeOriginalTopBarImages:(UIViewController*)viewController {
  155. NSMutableDictionary *originalTopBarImages = [@{} mutableCopy];
  156. UIImage *bgImage = [viewController.navigationController.navigationBar backgroundImageForBarMetrics:UIBarMetricsDefault];
  157. if (bgImage != nil) {
  158. originalTopBarImages[@"backgroundImage"] = bgImage;
  159. }
  160. UIImage *shadowImage = viewController.navigationController.navigationBar.shadowImage;
  161. if (shadowImage != nil) {
  162. originalTopBarImages[@"shadowImage"] = shadowImage;
  163. }
  164. self.originalTopBarImages = originalTopBarImages;
  165. }
  166. @end