react-native-navigation的迁移库

RNNNavigationOptions.m 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. #import "RNNNavigationOptions.h"
  2. #import <React/RCTConvert.h>
  3. #import "RNNNavigationController.h"
  4. #import "RNNTabBarController.h"
  5. #import "RNNTopBarOptions.h"
  6. const NSInteger BLUR_STATUS_TAG = 78264801;
  7. const NSInteger BLUR_TOPBAR_TAG = 78264802;
  8. @implementation RNNNavigationOptions
  9. -(instancetype)init {
  10. return [self initWithDict:@{}];
  11. }
  12. -(instancetype)initWithDict:(NSDictionary *)navigationOptions {
  13. self = [super init];
  14. self.statusBarHidden = [navigationOptions objectForKey:@"statusBarHidden"];
  15. self.screenBackgroundColor = [navigationOptions objectForKey:@"screenBackgroundColor"];
  16. self.tabBadge = [navigationOptions objectForKey:@"tabBadge"];
  17. self.orientation = [navigationOptions objectForKey:@"orientation"];
  18. self.leftButtons = [navigationOptions objectForKey:@"leftButtons"];
  19. self.rightButtons = [navigationOptions objectForKey:@"rightButtons"];
  20. self.tabBarHidden = [navigationOptions objectForKey:@"tabBarHidden"];
  21. self.topBar = [[RNNTopBarOptions alloc] initWithDict:[navigationOptions objectForKey:@"topBar"]];
  22. return self;
  23. }
  24. -(void)mergeWith:(NSDictionary *)otherOptions {
  25. for (id key in otherOptions) {
  26. if ([key isEqualToString:@"topBar"]) {
  27. [self.topBar mergeWith:[otherOptions objectForKey:@"topBar"]];
  28. } else {
  29. [self setValue:[otherOptions objectForKey:key] forKey:key];
  30. }
  31. }
  32. }
  33. -(void)applyOn:(UIViewController*)viewController {
  34. if (self.topBar) {
  35. if(self.topBar.backgroundColor) {
  36. UIColor* backgroundColor = [RCTConvert UIColor:self.topBar.backgroundColor];
  37. viewController.navigationController.navigationBar.barTintColor = backgroundColor;
  38. } else {
  39. viewController.navigationController.navigationBar.barTintColor = nil;
  40. }
  41. if (self.topBar.title) {
  42. viewController.navigationItem.title = self.topBar.title;
  43. }
  44. if (self.topBar.textFontFamily || self.topBar.textFontSize || self.topBar.textColor) {
  45. NSMutableDictionary* navigationBarTitleTextAttributes = [NSMutableDictionary new];
  46. if (self.topBar.textColor) {
  47. navigationBarTitleTextAttributes[NSForegroundColorAttributeName] = [RCTConvert UIColor:[self.topBar valueForKey:@"textColor"]];
  48. }
  49. if (self.topBar.textFontFamily){
  50. if(self.topBar.textFontSize) {
  51. navigationBarTitleTextAttributes[NSFontAttributeName] = [UIFont fontWithName:self.topBar.textFontFamily size:[self.topBar.textFontSize floatValue]];
  52. } else {
  53. navigationBarTitleTextAttributes[NSFontAttributeName] = [UIFont fontWithName:self.topBar.textFontFamily size:20];
  54. }
  55. } else if (self.topBar.textFontSize) {
  56. navigationBarTitleTextAttributes[NSFontAttributeName] = [UIFont systemFontOfSize:[self.topBar.textFontSize floatValue]];
  57. }
  58. viewController.navigationController.navigationBar.titleTextAttributes = navigationBarTitleTextAttributes;
  59. }
  60. if (self.topBar.hidden){
  61. [viewController.navigationController setNavigationBarHidden:[self.topBar.hidden boolValue] animated:[self.topBar.animateHide boolValue]];
  62. }
  63. if (self.topBar.hideOnScroll) {
  64. viewController.navigationController.hidesBarsOnSwipe = [self.topBar.hideOnScroll boolValue];
  65. }
  66. if (self.topBar.buttonColor) {
  67. UIColor* buttonColor = [RCTConvert UIColor:self.topBar.buttonColor];
  68. viewController.navigationController.navigationBar.tintColor = buttonColor;
  69. } else {
  70. viewController.navigationController.navigationBar.tintColor = nil;
  71. }
  72. if ([self.topBar.blur boolValue]) {
  73. if (![viewController.navigationController.navigationBar viewWithTag:BLUR_TOPBAR_TAG]) {
  74. [viewController.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
  75. viewController.navigationController.navigationBar.shadowImage = [UIImage new];
  76. UIVisualEffectView *blur = [[UIVisualEffectView alloc] initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleLight]];
  77. CGRect statusBarFrame = [[UIApplication sharedApplication] statusBarFrame];
  78. blur.frame = CGRectMake(0, -1 * statusBarFrame.size.height, viewController.navigationController.navigationBar.frame.size.width, viewController.navigationController.navigationBar.frame.size.height + statusBarFrame.size.height);
  79. blur.userInteractionEnabled = NO;
  80. blur.tag = BLUR_TOPBAR_TAG;
  81. [viewController.navigationController.navigationBar insertSubview:blur atIndex:0];
  82. [viewController.navigationController.navigationBar sendSubviewToBack:blur];
  83. }
  84. } else {
  85. UIView *blur = [viewController.navigationController.navigationBar viewWithTag:BLUR_TOPBAR_TAG];
  86. if (blur) {
  87. [viewController.navigationController.navigationBar setBackgroundImage: nil forBarMetrics:UIBarMetricsDefault];
  88. viewController.navigationController.navigationBar.shadowImage = nil;
  89. [blur removeFromSuperview];
  90. }
  91. }
  92. if (self.topBar.translucent) {
  93. viewController.navigationController.navigationBar.translucent = [self.topBar.translucent boolValue];
  94. }
  95. if (self.topBar.noBorder) {
  96. if ([self.topBar.noBorder boolValue]) {
  97. viewController.navigationController.navigationBar
  98. .shadowImage = [[UIImage alloc] init];
  99. } else {
  100. viewController.navigationController.navigationBar
  101. .shadowImage = nil;
  102. }
  103. }
  104. }
  105. if (self.screenBackgroundColor) {
  106. UIColor* screenColor = [RCTConvert UIColor:self.screenBackgroundColor];
  107. viewController.view.backgroundColor = screenColor;
  108. }
  109. if (self.tabBadge) {
  110. NSString *badge = [RCTConvert NSString:self.tabBadge];
  111. if (viewController.navigationController) {
  112. viewController.navigationController.tabBarItem.badgeValue = badge;
  113. } else {
  114. viewController.tabBarItem.badgeValue = badge;
  115. }
  116. }
  117. if (self.statusBarBlur) {
  118. UIView* curBlurView = [viewController.view viewWithTag:BLUR_STATUS_TAG];
  119. if ([self.statusBarBlur boolValue]) {
  120. if (!curBlurView) {
  121. UIVisualEffectView *blur = [[UIVisualEffectView alloc] initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleLight]];
  122. blur.frame = [[UIApplication sharedApplication] statusBarFrame];
  123. blur.tag = BLUR_STATUS_TAG;
  124. [viewController.view insertSubview:blur atIndex:0];
  125. }
  126. } else {
  127. if (curBlurView) {
  128. [curBlurView removeFromSuperview];
  129. }
  130. }
  131. }
  132. }
  133. - (UIInterfaceOrientationMask)supportedOrientations {
  134. NSArray* orientationsArray = [self.orientation isKindOfClass:[NSString class]] ? @[self.orientation] : self.orientation;
  135. NSUInteger supportedOrientationsMask = 0;
  136. if (!orientationsArray || [self.orientation isEqual:@"default"]) {
  137. return [[UIApplication sharedApplication] supportedInterfaceOrientationsForWindow:[[UIApplication sharedApplication] keyWindow]];
  138. } else {
  139. for (NSString* orientation in orientationsArray) {
  140. if ([orientation isEqualToString:@"all"]) {
  141. supportedOrientationsMask = UIInterfaceOrientationMaskAll;
  142. break;
  143. }
  144. if ([orientation isEqualToString:@"landscape"]) {
  145. supportedOrientationsMask = (supportedOrientationsMask | UIInterfaceOrientationMaskLandscape);
  146. }
  147. if ([orientation isEqualToString:@"portrait"]) {
  148. supportedOrientationsMask = (supportedOrientationsMask | UIInterfaceOrientationMaskPortrait);
  149. }
  150. if ([orientation isEqualToString:@"upsideDown"]) {
  151. supportedOrientationsMask = (supportedOrientationsMask | UIInterfaceOrientationMaskPortraitUpsideDown);
  152. }
  153. }
  154. }
  155. return supportedOrientationsMask;
  156. }
  157. @end