react-native-navigation的迁移库

RNNTopBarOptions.m 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. #import "RNNTopBarOptions.h"
  2. #import "RNNNavigationButtons.h"
  3. #import "RNNCustomTitleView.h"
  4. extern const NSInteger BLUR_TOPBAR_TAG;
  5. const NSInteger TOP_BAR_TRANSPARENT_TAG = 78264803;
  6. @interface RNNTopBarOptions ()
  7. @property (nonatomic, strong) NSMutableDictionary* originalTopBarImages;
  8. @property (nonatomic, strong) RNNNavigationButtons* navigationButtons;
  9. @end
  10. @implementation RNNTopBarOptions
  11. - (instancetype)initWithDict:(NSDictionary *)dict {
  12. self = [super initWithDict:dict];
  13. self.title.subtitle = self.subtitle;
  14. return self;
  15. }
  16. - (void)mergeWith:(NSDictionary *)otherOptions {
  17. [super mergeWith:otherOptions];
  18. self.title.subtitle = self.subtitle;
  19. }
  20. - (void)applyOn:(UIViewController*)viewController {
  21. [self.title applyOn:viewController];
  22. [self.largeTitle 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.drawBehind) {
  42. if ([self.drawBehind boolValue]) {
  43. viewController.edgesForExtendedLayout |= UIRectEdgeTop;
  44. } else {
  45. viewController.edgesForExtendedLayout &= ~UIRectEdgeTop;
  46. }
  47. } else {
  48. viewController.edgesForExtendedLayout = UIRectEdgeAll;
  49. }
  50. if (self.rightButtons || self.leftButtons) {
  51. _navigationButtons = [[RNNNavigationButtons alloc] initWithViewController:(RNNRootViewController*)viewController];
  52. [_navigationButtons applyLeftButtons:self.leftButtons rightButtons:self.rightButtons defaultLeftButtonStyle:self.leftButtonStyle defaultRightButtonStyle:self.rightButtonStyle];
  53. }
  54. [self applyOnNavigationController:viewController.navigationController];
  55. self.rightButtons = nil;
  56. self.leftButtons = nil;
  57. }
  58. - (void)applyOnNavigationController:(UINavigationController *)navigationController {
  59. [self.background applyOnNavigationController:navigationController];
  60. if (self.testID) {
  61. navigationController.navigationBar.accessibilityIdentifier = self.testID;
  62. }
  63. if (self.visible) {
  64. [navigationController setNavigationBarHidden:![self.visible boolValue] animated:[self.animate boolValue]];
  65. } else {
  66. [navigationController setNavigationBarHidden:NO animated:NO];
  67. }
  68. if (self.hideOnScroll) {
  69. navigationController.hidesBarsOnSwipe = [self.hideOnScroll boolValue];
  70. } else {
  71. navigationController.hidesBarsOnSwipe = NO;
  72. }
  73. if (self.noBorder) {
  74. if ([self.noBorder boolValue]) {
  75. navigationController.navigationBar
  76. .shadowImage = [[UIImage alloc] init];
  77. } else {
  78. navigationController.navigationBar
  79. .shadowImage = nil;
  80. }
  81. }
  82. if ([self.blur boolValue]) {
  83. if (![navigationController.navigationBar viewWithTag:BLUR_TOPBAR_TAG]) {
  84. [navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
  85. navigationController.navigationBar.shadowImage = [UIImage new];
  86. UIVisualEffectView *blur = [[UIVisualEffectView alloc] initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleLight]];
  87. CGRect statusBarFrame = [[UIApplication sharedApplication] statusBarFrame];
  88. blur.frame = CGRectMake(0, -1 * statusBarFrame.size.height, navigationController.navigationBar.frame.size.width, navigationController.navigationBar.frame.size.height + statusBarFrame.size.height);
  89. blur.userInteractionEnabled = NO;
  90. blur.tag = BLUR_TOPBAR_TAG;
  91. [navigationController.navigationBar insertSubview:blur atIndex:0];
  92. [navigationController.navigationBar sendSubviewToBack:blur];
  93. }
  94. } else {
  95. UIView *blur = [navigationController.navigationBar viewWithTag:BLUR_TOPBAR_TAG];
  96. if (blur) {
  97. [navigationController.navigationBar setBackgroundImage: nil forBarMetrics:UIBarMetricsDefault];
  98. navigationController.navigationBar.shadowImage = nil;
  99. [blur removeFromSuperview];
  100. }
  101. }
  102. void (^disableTopBarTransparent)(void) = ^ {
  103. UIView *transparentView = [navigationController.navigationBar viewWithTag:TOP_BAR_TRANSPARENT_TAG];
  104. if (transparentView){
  105. [transparentView removeFromSuperview];
  106. [navigationController.navigationBar setBackgroundImage:self.originalTopBarImages[@"backgroundImage"] forBarMetrics:UIBarMetricsDefault];
  107. navigationController.navigationBar.shadowImage = self.originalTopBarImages[@"shadowImage"];
  108. self.originalTopBarImages = nil;
  109. }
  110. };
  111. if (self.transparent) {
  112. if ([self.transparent boolValue]) {
  113. if (![navigationController.navigationBar viewWithTag:TOP_BAR_TRANSPARENT_TAG]){
  114. [self storeOriginalTopBarImages:navigationController];
  115. [navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
  116. navigationController.navigationBar.shadowImage = [UIImage new];
  117. UIView *transparentView = [[UIView alloc] initWithFrame:CGRectZero];
  118. transparentView.tag = TOP_BAR_TRANSPARENT_TAG;
  119. [navigationController.navigationBar insertSubview:transparentView atIndex:0];
  120. }
  121. } else {
  122. disableTopBarTransparent();
  123. }
  124. } else {
  125. disableTopBarTransparent();
  126. }
  127. if (self.barStyle) {
  128. navigationController.navigationBar.barStyle = [RCTConvert UIBarStyle:self.barStyle];
  129. } else {
  130. navigationController.navigationBar.barStyle = UIBarStyleDefault;
  131. }
  132. if (self.translucent) {
  133. navigationController.navigationBar.translucent = [self.translucent boolValue];
  134. } else {
  135. navigationController.navigationBar.translucent = NO;
  136. }
  137. }
  138. - (void)setRightButtonColor:(NSNumber *)rightButtonColor {
  139. _rightButtonColor = rightButtonColor;
  140. _rightButtonStyle.color = rightButtonColor;
  141. }
  142. - (void)setRightButtonDisabledColor:(NSNumber *)rightButtonDisabledColor {
  143. _rightButtonDisabledColor = rightButtonDisabledColor;
  144. _rightButtonStyle.disabledColor = rightButtonDisabledColor;
  145. }
  146. - (void)setLeftButtonColor:(NSNumber *)leftButtonColor {
  147. _leftButtonColor = leftButtonColor;
  148. _leftButtonStyle.color = leftButtonColor;
  149. }
  150. - (void)setLeftButtonDisabledColor:(NSNumber *)leftButtonDisabledColor {
  151. _leftButtonDisabledColor = leftButtonDisabledColor;
  152. _leftButtonStyle.disabledColor = leftButtonDisabledColor;
  153. }
  154. - (void)setRightButtons:(id)rightButtons {
  155. if ([rightButtons isKindOfClass:[NSArray class]]) {
  156. _rightButtons = rightButtons;
  157. } else if ([rightButtons isKindOfClass:[NSDictionary class]]) {
  158. if (rightButtons[@"id"]) {
  159. _rightButtons = @[rightButtons];
  160. } else {
  161. [_rightButtonStyle mergeWith:rightButtons];
  162. }
  163. } else {
  164. _rightButtons = rightButtons;
  165. }
  166. }
  167. - (void)setLeftButtons:(id)leftButtons {
  168. if ([leftButtons isKindOfClass:[NSArray class]]) {
  169. _leftButtons = leftButtons;
  170. } else if ([leftButtons isKindOfClass:[NSDictionary class]]) {
  171. if (leftButtons[@"id"]) {
  172. _leftButtons = @[leftButtons];
  173. } else {
  174. [_leftButtonStyle mergeWith:leftButtons];
  175. }
  176. } else {
  177. _leftButtons = leftButtons;
  178. }
  179. }
  180. -(void)storeOriginalTopBarImages:(UINavigationController *)navigationController {
  181. NSMutableDictionary *originalTopBarImages = [@{} mutableCopy];
  182. UIImage *bgImage = [navigationController.navigationBar backgroundImageForBarMetrics:UIBarMetricsDefault];
  183. if (bgImage != nil) {
  184. originalTopBarImages[@"backgroundImage"] = bgImage;
  185. }
  186. UIImage *shadowImage = navigationController.navigationBar.shadowImage;
  187. if (shadowImage != nil) {
  188. originalTopBarImages[@"shadowImage"] = shadowImage;
  189. }
  190. self.originalTopBarImages = originalTopBarImages;
  191. }
  192. @end