react-native-navigation的迁移库

RNNTopBarOptions.m 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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 = [RNNTitleOptions new];
  13. return self;
  14. }
  15. - (void)applyOn:(UIViewController*)viewController {
  16. [self.title applyOn:viewController];
  17. if (self.backgroundColor) {
  18. UIColor* backgroundColor = [RCTConvert UIColor:self.backgroundColor];
  19. viewController.navigationController.navigationBar.barTintColor = backgroundColor;
  20. } else {
  21. viewController.navigationController.navigationBar.barTintColor = nil;
  22. }
  23. if (@available(iOS 11.0, *)) {
  24. if (self.largeTitle){
  25. if ([self.largeTitle boolValue]) {
  26. viewController.navigationController.navigationBar.prefersLargeTitles = YES;
  27. viewController.navigationItem.largeTitleDisplayMode = UINavigationItemLargeTitleDisplayModeAlways;
  28. } else {
  29. viewController.navigationItem.largeTitleDisplayMode = UINavigationItemLargeTitleDisplayModeNever;
  30. }
  31. } else {
  32. viewController.navigationController.navigationBar.prefersLargeTitles = NO;
  33. viewController.navigationItem.largeTitleDisplayMode = UINavigationItemLargeTitleDisplayModeNever;
  34. }
  35. }
  36. if (self.visible) {
  37. [viewController.navigationController setNavigationBarHidden:![self.visible boolValue] animated:[self.animate boolValue]];
  38. } else {
  39. [viewController.navigationController setNavigationBarHidden:NO animated:NO];
  40. }
  41. if (self.hideOnScroll) {
  42. viewController.navigationController.hidesBarsOnSwipe = [self.hideOnScroll boolValue];
  43. } else {
  44. viewController.navigationController.hidesBarsOnSwipe = NO;
  45. }
  46. if (self.buttonColor) {
  47. UIColor* buttonColor = [RCTConvert UIColor:self.buttonColor];
  48. viewController.navigationController.navigationBar.tintColor = buttonColor;
  49. } else {
  50. viewController.navigationController.navigationBar.tintColor = nil;
  51. }
  52. if ([self.blur boolValue]) {
  53. if (![viewController.navigationController.navigationBar viewWithTag:BLUR_TOPBAR_TAG]) {
  54. [viewController.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
  55. viewController.navigationController.navigationBar.shadowImage = [UIImage new];
  56. UIVisualEffectView *blur = [[UIVisualEffectView alloc] initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleLight]];
  57. CGRect statusBarFrame = [[UIApplication sharedApplication] statusBarFrame];
  58. blur.frame = CGRectMake(0, -1 * statusBarFrame.size.height, viewController.navigationController.navigationBar.frame.size.width, viewController.navigationController.navigationBar.frame.size.height + statusBarFrame.size.height);
  59. blur.userInteractionEnabled = NO;
  60. blur.tag = BLUR_TOPBAR_TAG;
  61. [viewController.navigationController.navigationBar insertSubview:blur atIndex:0];
  62. [viewController.navigationController.navigationBar sendSubviewToBack:blur];
  63. }
  64. } else {
  65. UIView *blur = [viewController.navigationController.navigationBar viewWithTag:BLUR_TOPBAR_TAG];
  66. if (blur) {
  67. [viewController.navigationController.navigationBar setBackgroundImage: nil forBarMetrics:UIBarMetricsDefault];
  68. viewController.navigationController.navigationBar.shadowImage = nil;
  69. [blur removeFromSuperview];
  70. }
  71. }
  72. void (^disableTopBarTransparent)(void) = ^ {
  73. UIView *transparentView = [viewController.navigationController.navigationBar viewWithTag:TOP_BAR_TRANSPARENT_TAG];
  74. if (transparentView){
  75. [transparentView removeFromSuperview];
  76. [viewController.navigationController.navigationBar setBackgroundImage:self.originalTopBarImages[@"backgroundImage"] forBarMetrics:UIBarMetricsDefault];
  77. viewController.navigationController.navigationBar.shadowImage = self.originalTopBarImages[@"shadowImage"];
  78. self.originalTopBarImages = nil;
  79. }
  80. };
  81. if (self.transparent) {
  82. if ([self.transparent boolValue]) {
  83. if (![viewController.navigationController.navigationBar viewWithTag:TOP_BAR_TRANSPARENT_TAG]){
  84. [self storeOriginalTopBarImages:viewController];
  85. [viewController.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
  86. viewController.navigationController.navigationBar.shadowImage = [UIImage new];
  87. UIView *transparentView = [[UIView alloc] initWithFrame:CGRectZero];
  88. transparentView.tag = TOP_BAR_TRANSPARENT_TAG;
  89. [viewController.navigationController.navigationBar insertSubview:transparentView atIndex:0];
  90. }
  91. } else {
  92. disableTopBarTransparent();
  93. }
  94. } else {
  95. disableTopBarTransparent();
  96. }
  97. if (self.translucent) {
  98. viewController.navigationController.navigationBar.translucent = [self.translucent boolValue];
  99. } else {
  100. viewController.navigationController.navigationBar.translucent = NO;
  101. }
  102. if (self.drawBehind) {
  103. if ([self.drawBehind boolValue]) {
  104. viewController.edgesForExtendedLayout |= UIRectEdgeTop;
  105. } else {
  106. viewController.edgesForExtendedLayout &= ~UIRectEdgeTop;
  107. }
  108. } else {
  109. viewController.edgesForExtendedLayout = UIRectEdgeAll;
  110. }
  111. if (self.noBorder) {
  112. if ([self.noBorder boolValue]) {
  113. viewController.navigationController.navigationBar
  114. .shadowImage = [[UIImage alloc] init];
  115. } else {
  116. viewController.navigationController.navigationBar
  117. .shadowImage = nil;
  118. }
  119. }
  120. if (self.testID) {
  121. viewController.navigationController.navigationBar.accessibilityIdentifier = self.testID;
  122. }
  123. if (self.rightButtons || self.leftButtons) {
  124. _navigationButtons = [[RNNNavigationButtons alloc] initWithViewController:(RNNRootViewController*)viewController];
  125. [_navigationButtons applyLeftButtons:self.leftButtons rightButtons:self.rightButtons];
  126. }
  127. UIImage *image = self.backButtonImage ? [RCTConvert UIImage:self.backButtonImage] : nil;
  128. [[UINavigationBar appearance] setBackIndicatorImage:image];
  129. [[UINavigationBar appearance] setBackIndicatorTransitionMaskImage:image];
  130. if (self.hideBackButtonTitle) {
  131. self.backButtonTitle = @"";
  132. }
  133. if (self.backButtonTitle) {
  134. UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithTitle:self.backButtonTitle
  135. style:UIBarButtonItemStylePlain
  136. target:nil
  137. action:nil];
  138. viewController.navigationItem.backBarButtonItem = backItem;
  139. }
  140. viewController.navigationItem.hidesBackButton = [self.backButtonHidden boolValue];
  141. }
  142. -(void)storeOriginalTopBarImages:(UIViewController*)viewController {
  143. NSMutableDictionary *originalTopBarImages = [@{} mutableCopy];
  144. UIImage *bgImage = [viewController.navigationController.navigationBar backgroundImageForBarMetrics:UIBarMetricsDefault];
  145. if (bgImage != nil) {
  146. originalTopBarImages[@"backgroundImage"] = bgImage;
  147. }
  148. UIImage *shadowImage = viewController.navigationController.navigationBar.shadowImage;
  149. if (shadowImage != nil) {
  150. originalTopBarImages[@"shadowImage"] = shadowImage;
  151. }
  152. self.originalTopBarImages = originalTopBarImages;
  153. }
  154. @end