react-native-navigation的迁移库

RNNTopBarOptions.m 8.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. self.rightButtons = nil;
  132. self.leftButtons = nil;
  133. }
  134. - (void)setRightButtonColor:(NSNumber *)rightButtonColor {
  135. _rightButtonColor = rightButtonColor;
  136. _rightButtonStyle.color = rightButtonColor;
  137. }
  138. - (void)setRightButtonDisabledColor:(NSNumber *)rightButtonDisabledColor {
  139. _rightButtonDisabledColor = rightButtonDisabledColor;
  140. _rightButtonStyle.disabledColor = rightButtonDisabledColor;
  141. }
  142. - (void)setLeftButtonColor:(NSNumber *)leftButtonColor {
  143. _leftButtonColor = leftButtonColor;
  144. _leftButtonStyle.color = leftButtonColor;
  145. }
  146. - (void)setLeftButtonDisabledColor:(NSNumber *)leftButtonDisabledColor {
  147. _leftButtonDisabledColor = leftButtonDisabledColor;
  148. _leftButtonStyle.disabledColor = leftButtonDisabledColor;
  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. } else {
  160. _rightButtons = rightButtons;
  161. }
  162. }
  163. - (void)setLeftButtons:(id)leftButtons {
  164. if ([leftButtons isKindOfClass:[NSArray class]]) {
  165. _leftButtons = leftButtons;
  166. } else if ([leftButtons isKindOfClass:[NSDictionary class]]) {
  167. if (leftButtons[@"id"]) {
  168. _leftButtons = @[leftButtons];
  169. } else {
  170. [_leftButtonStyle mergeWith:leftButtons];
  171. }
  172. } else {
  173. _leftButtons = leftButtons;
  174. }
  175. }
  176. -(void)storeOriginalTopBarImages:(UIViewController*)viewController {
  177. NSMutableDictionary *originalTopBarImages = [@{} mutableCopy];
  178. UIImage *bgImage = [viewController.navigationController.navigationBar backgroundImageForBarMetrics:UIBarMetricsDefault];
  179. if (bgImage != nil) {
  180. originalTopBarImages[@"backgroundImage"] = bgImage;
  181. }
  182. UIImage *shadowImage = viewController.navigationController.navigationBar.shadowImage;
  183. if (shadowImage != nil) {
  184. originalTopBarImages[@"shadowImage"] = shadowImage;
  185. }
  186. self.originalTopBarImages = originalTopBarImages;
  187. }
  188. @end