react-native-navigation的迁移库

RNNTopBarOptions.m 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. }
  39. }
  40. if (self.visible) {
  41. [viewController.navigationController setNavigationBarHidden:![self.visible boolValue] animated:[self.animate boolValue]];
  42. } else {
  43. [viewController.navigationController setNavigationBarHidden:NO animated:NO];
  44. }
  45. if (self.hideOnScroll) {
  46. viewController.navigationController.hidesBarsOnSwipe = [self.hideOnScroll boolValue];
  47. } else {
  48. viewController.navigationController.hidesBarsOnSwipe = NO;
  49. }
  50. if (self.buttonColor) {
  51. UIColor* buttonColor = [RCTConvert UIColor:self.buttonColor];
  52. viewController.navigationController.navigationBar.tintColor = buttonColor;
  53. } else {
  54. viewController.navigationController.navigationBar.tintColor = nil;
  55. }
  56. if ([self.blur boolValue]) {
  57. if (![viewController.navigationController.navigationBar viewWithTag:BLUR_TOPBAR_TAG]) {
  58. [viewController.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
  59. viewController.navigationController.navigationBar.shadowImage = [UIImage new];
  60. UIVisualEffectView *blur = [[UIVisualEffectView alloc] initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleLight]];
  61. CGRect statusBarFrame = [[UIApplication sharedApplication] statusBarFrame];
  62. blur.frame = CGRectMake(0, -1 * statusBarFrame.size.height, viewController.navigationController.navigationBar.frame.size.width, viewController.navigationController.navigationBar.frame.size.height + statusBarFrame.size.height);
  63. blur.userInteractionEnabled = NO;
  64. blur.tag = BLUR_TOPBAR_TAG;
  65. [viewController.navigationController.navigationBar insertSubview:blur atIndex:0];
  66. [viewController.navigationController.navigationBar sendSubviewToBack:blur];
  67. }
  68. } else {
  69. UIView *blur = [viewController.navigationController.navigationBar viewWithTag:BLUR_TOPBAR_TAG];
  70. if (blur) {
  71. [viewController.navigationController.navigationBar setBackgroundImage: nil forBarMetrics:UIBarMetricsDefault];
  72. viewController.navigationController.navigationBar.shadowImage = nil;
  73. [blur removeFromSuperview];
  74. }
  75. }
  76. void (^disableTopBarTransparent)(void) = ^ {
  77. UIView *transparentView = [viewController.navigationController.navigationBar viewWithTag:TOP_BAR_TRANSPARENT_TAG];
  78. if (transparentView){
  79. [transparentView removeFromSuperview];
  80. [viewController.navigationController.navigationBar setBackgroundImage:self.originalTopBarImages[@"backgroundImage"] forBarMetrics:UIBarMetricsDefault];
  81. viewController.navigationController.navigationBar.shadowImage = self.originalTopBarImages[@"shadowImage"];
  82. self.originalTopBarImages = nil;
  83. }
  84. };
  85. if (self.transparent) {
  86. if ([self.transparent boolValue]) {
  87. if (![viewController.navigationController.navigationBar viewWithTag:TOP_BAR_TRANSPARENT_TAG]){
  88. [self storeOriginalTopBarImages:viewController];
  89. [viewController.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
  90. viewController.navigationController.navigationBar.shadowImage = [UIImage new];
  91. UIView *transparentView = [[UIView alloc] initWithFrame:CGRectZero];
  92. transparentView.tag = TOP_BAR_TRANSPARENT_TAG;
  93. [viewController.navigationController.navigationBar insertSubview:transparentView atIndex:0];
  94. }
  95. } else {
  96. disableTopBarTransparent();
  97. }
  98. } else {
  99. disableTopBarTransparent();
  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 defaultButtonStyle:_button];
  130. }
  131. }
  132. -(void)storeOriginalTopBarImages:(UIViewController*)viewController {
  133. NSMutableDictionary *originalTopBarImages = [@{} mutableCopy];
  134. UIImage *bgImage = [viewController.navigationController.navigationBar backgroundImageForBarMetrics:UIBarMetricsDefault];
  135. if (bgImage != nil) {
  136. originalTopBarImages[@"backgroundImage"] = bgImage;
  137. }
  138. UIImage *shadowImage = viewController.navigationController.navigationBar.shadowImage;
  139. if (shadowImage != nil) {
  140. originalTopBarImages[@"shadowImage"] = shadowImage;
  141. }
  142. self.originalTopBarImages = originalTopBarImages;
  143. }
  144. @end