react-native-navigation的迁移库

RNNNavigationOptions.m 7.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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.noBorder) {
  97. if ([self.topBar.noBorder boolValue]) {
  98. viewController.navigationController.navigationBar
  99. .shadowImage = [[UIImage alloc] init];
  100. } else {
  101. viewController.navigationController.navigationBar
  102. .shadowImage = nil;
  103. }
  104. }
  105. }
  106. if (self.screenBackgroundColor) {
  107. UIColor* screenColor = [RCTConvert UIColor:self.screenBackgroundColor];
  108. viewController.view.backgroundColor = screenColor;
  109. }
  110. if (self.tabBar) {
  111. if (self.tabBar.tabBadge) {
  112. NSString *badge = [RCTConvert NSString:self.tabBar.tabBadge];
  113. if (viewController.navigationController) {
  114. viewController.navigationController.tabBarItem.badgeValue = badge;
  115. } else {
  116. viewController.tabBarItem.badgeValue = badge;
  117. }
  118. }
  119. if (self.tabBar.currentTabIndex) {
  120. [viewController.tabBarController setSelectedIndex:[self.tabBar.currentTabIndex unsignedIntegerValue]];
  121. }
  122. }
  123. if (self.statusBarBlur) {
  124. UIView* curBlurView = [viewController.view viewWithTag:BLUR_STATUS_TAG];
  125. if ([self.statusBarBlur boolValue]) {
  126. if (!curBlurView) {
  127. UIVisualEffectView *blur = [[UIVisualEffectView alloc] initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleLight]];
  128. blur.frame = [[UIApplication sharedApplication] statusBarFrame];
  129. blur.tag = BLUR_STATUS_TAG;
  130. [viewController.view insertSubview:blur atIndex:0];
  131. }
  132. } else {
  133. if (curBlurView) {
  134. [curBlurView removeFromSuperview];
  135. }
  136. }
  137. }
  138. }
  139. - (UIInterfaceOrientationMask)supportedOrientations {
  140. NSArray* orientationsArray = [self.orientation isKindOfClass:[NSString class]] ? @[self.orientation] : self.orientation;
  141. NSUInteger supportedOrientationsMask = 0;
  142. if (!orientationsArray || [self.orientation isEqual:@"default"]) {
  143. return [[UIApplication sharedApplication] supportedInterfaceOrientationsForWindow:[[UIApplication sharedApplication] keyWindow]];
  144. } else {
  145. for (NSString* orientation in orientationsArray) {
  146. if ([orientation isEqualToString:@"all"]) {
  147. supportedOrientationsMask = UIInterfaceOrientationMaskAll;
  148. break;
  149. }
  150. if ([orientation isEqualToString:@"landscape"]) {
  151. supportedOrientationsMask = (supportedOrientationsMask | UIInterfaceOrientationMaskLandscape);
  152. }
  153. if ([orientation isEqualToString:@"portrait"]) {
  154. supportedOrientationsMask = (supportedOrientationsMask | UIInterfaceOrientationMaskPortrait);
  155. }
  156. if ([orientation isEqualToString:@"upsideDown"]) {
  157. supportedOrientationsMask = (supportedOrientationsMask | UIInterfaceOrientationMaskPortraitUpsideDown);
  158. }
  159. }
  160. }
  161. return supportedOrientationsMask;
  162. }
  163. @end