react-native-navigation的迁移库

RNNNavigationOptions.m 7.8KB

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