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.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.buttonColor) {
  128. UIColor* buttonColor = [RCTConvert UIColor:self.buttonColor];
  129. viewController.navigationController.navigationBar.tintColor = buttonColor;
  130. } else {
  131. viewController.navigationController.navigationBar.tintColor = nil;
  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