react-native-navigation的迁移库

RNNNavigationOptions.m 7.9KB

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