react-native-navigation的迁移库

RNNNavigationOptions.m 7.9KB

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