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