react-native-navigation的迁移库

RNNTopBarOptions.m 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. // enable it back if needed on componentDidAppear
  37. viewController.navigationItem.hidesSearchBarWhenScrolling = NO;
  38. // Fixes #3450, otherwise, UIKit will infer the presentation context to be the root most view controller
  39. viewController.definesPresentationContext = YES;
  40. }
  41. }
  42. if (self.visible) {
  43. [viewController.navigationController setNavigationBarHidden:![self.visible boolValue] animated:[self.animate boolValue]];
  44. } else {
  45. [viewController.navigationController setNavigationBarHidden:NO animated:NO];
  46. }
  47. if (self.hideOnScroll) {
  48. viewController.navigationController.hidesBarsOnSwipe = [self.hideOnScroll boolValue];
  49. } else {
  50. viewController.navigationController.hidesBarsOnSwipe = NO;
  51. }
  52. if (self.buttonColor) {
  53. UIColor* buttonColor = [RCTConvert UIColor:self.buttonColor];
  54. viewController.navigationController.navigationBar.tintColor = buttonColor;
  55. } else {
  56. viewController.navigationController.navigationBar.tintColor = nil;
  57. }
  58. if ([self.blur boolValue]) {
  59. if (![viewController.navigationController.navigationBar viewWithTag:BLUR_TOPBAR_TAG]) {
  60. [viewController.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
  61. viewController.navigationController.navigationBar.shadowImage = [UIImage new];
  62. UIVisualEffectView *blur = [[UIVisualEffectView alloc] initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleLight]];
  63. CGRect statusBarFrame = [[UIApplication sharedApplication] statusBarFrame];
  64. blur.frame = CGRectMake(0, -1 * statusBarFrame.size.height, viewController.navigationController.navigationBar.frame.size.width, viewController.navigationController.navigationBar.frame.size.height + statusBarFrame.size.height);
  65. blur.userInteractionEnabled = NO;
  66. blur.tag = BLUR_TOPBAR_TAG;
  67. [viewController.navigationController.navigationBar insertSubview:blur atIndex:0];
  68. [viewController.navigationController.navigationBar sendSubviewToBack:blur];
  69. }
  70. } else {
  71. UIView *blur = [viewController.navigationController.navigationBar viewWithTag:BLUR_TOPBAR_TAG];
  72. if (blur) {
  73. [viewController.navigationController.navigationBar setBackgroundImage: nil forBarMetrics:UIBarMetricsDefault];
  74. viewController.navigationController.navigationBar.shadowImage = nil;
  75. [blur removeFromSuperview];
  76. }
  77. }
  78. void (^disableTopBarTransparent)(void) = ^ {
  79. UIView *transparentView = [viewController.navigationController.navigationBar viewWithTag:TOP_BAR_TRANSPARENT_TAG];
  80. if (transparentView){
  81. [transparentView removeFromSuperview];
  82. [viewController.navigationController.navigationBar setBackgroundImage:self.originalTopBarImages[@"backgroundImage"] forBarMetrics:UIBarMetricsDefault];
  83. viewController.navigationController.navigationBar.shadowImage = self.originalTopBarImages[@"shadowImage"];
  84. self.originalTopBarImages = nil;
  85. }
  86. };
  87. if (self.transparent) {
  88. if ([self.transparent boolValue]) {
  89. if (![viewController.navigationController.navigationBar viewWithTag:TOP_BAR_TRANSPARENT_TAG]){
  90. [self storeOriginalTopBarImages:viewController];
  91. [viewController.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
  92. viewController.navigationController.navigationBar.shadowImage = [UIImage new];
  93. UIView *transparentView = [[UIView alloc] initWithFrame:CGRectZero];
  94. transparentView.tag = TOP_BAR_TRANSPARENT_TAG;
  95. [viewController.navigationController.navigationBar insertSubview:transparentView atIndex:0];
  96. }
  97. } else {
  98. disableTopBarTransparent();
  99. }
  100. } else {
  101. disableTopBarTransparent();
  102. }
  103. if (self.barStyle) {
  104. viewController.navigationController.navigationBar.barStyle = [RCTConvert UIBarStyle:self.barStyle];
  105. } else {
  106. viewController.navigationController.navigationBar.barStyle = UIBarStyleDefault;
  107. }
  108. if (self.translucent) {
  109. viewController.navigationController.navigationBar.translucent = [self.translucent boolValue];
  110. } else {
  111. viewController.navigationController.navigationBar.translucent = NO;
  112. }
  113. if (self.drawBehind) {
  114. if ([self.drawBehind boolValue]) {
  115. viewController.edgesForExtendedLayout |= UIRectEdgeTop;
  116. } else {
  117. viewController.edgesForExtendedLayout &= ~UIRectEdgeTop;
  118. }
  119. } else {
  120. viewController.edgesForExtendedLayout = UIRectEdgeAll;
  121. }
  122. if (self.noBorder) {
  123. if ([self.noBorder boolValue]) {
  124. viewController.navigationController.navigationBar
  125. .shadowImage = [[UIImage alloc] init];
  126. } else {
  127. viewController.navigationController.navigationBar
  128. .shadowImage = nil;
  129. }
  130. }
  131. if (self.testID) {
  132. viewController.navigationController.navigationBar.accessibilityIdentifier = self.testID;
  133. }
  134. if (self.rightButtons || self.leftButtons) {
  135. _navigationButtons = [[RNNNavigationButtons alloc] initWithViewController:(RNNRootViewController*)viewController];
  136. [_navigationButtons applyLeftButtons:self.leftButtons rightButtons:self.rightButtons defaultLeftButtonStyle:self.leftButtonStyle defaultRightButtonStyle:self.rightButtonStyle];
  137. }
  138. }
  139. - (void)setLeftButtons:(id)leftButtons {
  140. if ([leftButtons isKindOfClass:[NSArray class]]) {
  141. _leftButtons = leftButtons;
  142. } else if ([leftButtons isKindOfClass:[NSDictionary class]]) {
  143. if (leftButtons[@"id"]) {
  144. _leftButtons = @[leftButtons];
  145. } else {
  146. [_leftButtonStyle mergeWith:leftButtons];
  147. }
  148. }
  149. }
  150. - (void)setRightButtons:(id)rightButtons {
  151. if ([rightButtons isKindOfClass:[NSArray class]]) {
  152. _rightButtons = rightButtons;
  153. } else if ([rightButtons isKindOfClass:[NSDictionary class]]) {
  154. if (rightButtons[@"id"]) {
  155. _rightButtons = @[rightButtons];
  156. } else {
  157. [_rightButtonStyle mergeWith:rightButtons];
  158. }
  159. }
  160. }
  161. -(void)storeOriginalTopBarImages:(UIViewController*)viewController {
  162. NSMutableDictionary *originalTopBarImages = [@{} mutableCopy];
  163. UIImage *bgImage = [viewController.navigationController.navigationBar backgroundImageForBarMetrics:UIBarMetricsDefault];
  164. if (bgImage != nil) {
  165. originalTopBarImages[@"backgroundImage"] = bgImage;
  166. }
  167. UIImage *shadowImage = viewController.navigationController.navigationBar.shadowImage;
  168. if (shadowImage != nil) {
  169. originalTopBarImages[@"shadowImage"] = shadowImage;
  170. }
  171. self.originalTopBarImages = originalTopBarImages;
  172. }
  173. @end