react-native-navigation的迁移库

RNNNavigationOptions.m 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. const NSInteger TOP_BAR_TRANSPARENT_TAG = 78264803;
  9. @implementation RNNNavigationOptions
  10. -(instancetype)init {
  11. return [self initWithDict:@{}];
  12. }
  13. -(instancetype)initWithDict:(NSDictionary *)navigationOptions {
  14. self = [super init];
  15. self.statusBarHidden = [navigationOptions objectForKey:@"statusBarHidden"];
  16. self.screenBackgroundColor = [navigationOptions objectForKey:@"screenBackgroundColor"];
  17. self.backButtonTransition = [navigationOptions objectForKey:@"backButtonTransition"];
  18. self.orientation = [navigationOptions objectForKey:@"orientation"];
  19. self.leftButtons = [navigationOptions objectForKey:@"leftButtons"];
  20. self.rightButtons = [navigationOptions objectForKey:@"rightButtons"];
  21. self.topBar = [[RNNTopBarOptions alloc] initWithDict:[navigationOptions objectForKey:@"topBar"]];
  22. self.bottomTabs = [[RNNTabBarOptions alloc] initWithDict:[navigationOptions objectForKey:@"bottomTabs"]];
  23. return self;
  24. }
  25. -(void)mergeWith:(NSDictionary *)otherOptions {
  26. for (id key in otherOptions) {
  27. if ([key isEqualToString:@"topBar"]) {
  28. [self.topBar mergeWith:[otherOptions objectForKey:key]];
  29. } else if ([key isEqualToString:@"bottomTabs"]) {
  30. [self.bottomTabs mergeWith:[otherOptions objectForKey:key]];
  31. } else {
  32. [self setValue:[otherOptions objectForKey:key] forKey:key];
  33. }
  34. }
  35. }
  36. -(void)applyOn:(UIViewController*)viewController {
  37. if (self.topBar) {
  38. if(self.topBar.backgroundColor) {
  39. UIColor* backgroundColor = [RCTConvert UIColor:self.topBar.backgroundColor];
  40. viewController.navigationController.navigationBar.barTintColor = backgroundColor;
  41. } else {
  42. viewController.navigationController.navigationBar.barTintColor = nil;
  43. }
  44. if (self.topBar.title) {
  45. viewController.navigationItem.title = self.topBar.title;
  46. }
  47. if (@available(iOS 11.0, *)) {
  48. if (self.topBar.largeTitle){
  49. if ([self.topBar.largeTitle boolValue]) {
  50. viewController.navigationController.navigationBar.prefersLargeTitles = YES;
  51. viewController.navigationItem.largeTitleDisplayMode = UINavigationItemLargeTitleDisplayModeAlways;
  52. } else {
  53. viewController.navigationItem.largeTitleDisplayMode = UINavigationItemLargeTitleDisplayModeNever;
  54. }
  55. } else {
  56. viewController.navigationController.navigationBar.prefersLargeTitles = NO;
  57. viewController.navigationItem.largeTitleDisplayMode = UINavigationItemLargeTitleDisplayModeNever;
  58. }
  59. }
  60. if (self.topBar.textFontFamily || self.topBar.textFontSize || self.topBar.textColor) {
  61. NSMutableDictionary* navigationBarTitleTextAttributes = [NSMutableDictionary new];
  62. if (self.topBar.textColor) {
  63. navigationBarTitleTextAttributes[NSForegroundColorAttributeName] = [RCTConvert UIColor:[self.topBar valueForKey:@"textColor"]];
  64. }
  65. if (self.topBar.textFontFamily){
  66. if(self.topBar.textFontSize) {
  67. navigationBarTitleTextAttributes[NSFontAttributeName] = [UIFont fontWithName:self.topBar.textFontFamily size:[self.topBar.textFontSize floatValue]];
  68. } else {
  69. navigationBarTitleTextAttributes[NSFontAttributeName] = [UIFont fontWithName:self.topBar.textFontFamily size:20];
  70. }
  71. } else if (self.topBar.textFontSize) {
  72. navigationBarTitleTextAttributes[NSFontAttributeName] = [UIFont systemFontOfSize:[self.topBar.textFontSize floatValue]];
  73. }
  74. viewController.navigationController.navigationBar.titleTextAttributes = navigationBarTitleTextAttributes;
  75. }
  76. if (self.topBar.hidden){
  77. [viewController.navigationController setNavigationBarHidden:[self.topBar.hidden boolValue] animated:[self.topBar.animateHide boolValue]];
  78. }
  79. if (self.topBar.hideOnScroll) {
  80. viewController.navigationController.hidesBarsOnSwipe = [self.topBar.hideOnScroll boolValue];
  81. }
  82. if (self.topBar.buttonColor) {
  83. UIColor* buttonColor = [RCTConvert UIColor:self.topBar.buttonColor];
  84. viewController.navigationController.navigationBar.tintColor = buttonColor;
  85. } else {
  86. viewController.navigationController.navigationBar.tintColor = nil;
  87. }
  88. if ([self.topBar.blur boolValue]) {
  89. if (![viewController.navigationController.navigationBar viewWithTag:BLUR_TOPBAR_TAG]) {
  90. [viewController.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
  91. viewController.navigationController.navigationBar.shadowImage = [UIImage new];
  92. UIVisualEffectView *blur = [[UIVisualEffectView alloc] initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleLight]];
  93. CGRect statusBarFrame = [[UIApplication sharedApplication] statusBarFrame];
  94. blur.frame = CGRectMake(0, -1 * statusBarFrame.size.height, viewController.navigationController.navigationBar.frame.size.width, viewController.navigationController.navigationBar.frame.size.height + statusBarFrame.size.height);
  95. blur.userInteractionEnabled = NO;
  96. blur.tag = BLUR_TOPBAR_TAG;
  97. [viewController.navigationController.navigationBar insertSubview:blur atIndex:0];
  98. [viewController.navigationController.navigationBar sendSubviewToBack:blur];
  99. }
  100. } else {
  101. UIView *blur = [viewController.navigationController.navigationBar viewWithTag:BLUR_TOPBAR_TAG];
  102. if (blur) {
  103. [viewController.navigationController.navigationBar setBackgroundImage: nil forBarMetrics:UIBarMetricsDefault];
  104. viewController.navigationController.navigationBar.shadowImage = nil;
  105. [blur removeFromSuperview];
  106. }
  107. }
  108. void (^disableTopBarTransparent)() = ^void(){
  109. UIView *transparentView = [viewController.navigationController.navigationBar viewWithTag:TOP_BAR_TRANSPARENT_TAG];
  110. if (transparentView){
  111. [transparentView removeFromSuperview];
  112. [viewController.navigationController.navigationBar setBackgroundImage:self.originalTopBarImages[@"backgroundImage"] forBarMetrics:UIBarMetricsDefault];
  113. viewController.navigationController.navigationBar.shadowImage = self.originalTopBarImages[@"shadowImage"];
  114. self.originalTopBarImages = nil;
  115. }
  116. };
  117. if (self.topBar.transparent) {
  118. if ([self.topBar.transparent boolValue]) {
  119. if (![viewController.navigationController.navigationBar viewWithTag:TOP_BAR_TRANSPARENT_TAG]){
  120. [self storeOriginalTopBarImages:viewController];
  121. [viewController.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
  122. viewController.navigationController.navigationBar.shadowImage = [UIImage new];
  123. UIView *transparentView = [[UIView alloc] initWithFrame:CGRectZero];
  124. transparentView.tag = TOP_BAR_TRANSPARENT_TAG;
  125. [viewController.navigationController.navigationBar insertSubview:transparentView atIndex:0];
  126. }
  127. } else {
  128. disableTopBarTransparent();
  129. }
  130. } else {
  131. disableTopBarTransparent();
  132. }
  133. if (self.topBar.translucent) {
  134. viewController.navigationController.navigationBar.translucent = [self.topBar.translucent boolValue];
  135. }
  136. if (self.topBar.noBorder) {
  137. if ([self.topBar.noBorder boolValue]) {
  138. viewController.navigationController.navigationBar
  139. .shadowImage = [[UIImage alloc] init];
  140. } else {
  141. viewController.navigationController.navigationBar
  142. .shadowImage = nil;
  143. }
  144. }
  145. }
  146. if (self.popGesture) {
  147. viewController.navigationController.interactivePopGestureRecognizer.enabled = [self.popGesture boolValue];
  148. }
  149. if (self.screenBackgroundColor) {
  150. UIColor* screenColor = [RCTConvert UIColor:self.screenBackgroundColor];
  151. viewController.view.backgroundColor = screenColor;
  152. }
  153. if (self.bottomTabs) {
  154. if (self.bottomTabs.tabBadge) {
  155. NSString *badge = [RCTConvert NSString:self.bottomTabs.tabBadge];
  156. if (viewController.navigationController) {
  157. viewController.navigationController.tabBarItem.badgeValue = badge;
  158. } else {
  159. viewController.tabBarItem.badgeValue = badge;
  160. }
  161. }
  162. if (self.bottomTabs.currentTabIndex) {
  163. [viewController.tabBarController setSelectedIndex:[self.bottomTabs.currentTabIndex unsignedIntegerValue]];
  164. }
  165. if (self.bottomTabs.hidden) {
  166. [((RNNTabBarController *)viewController.tabBarController) setTabBarHidden:[self.bottomTabs.hidden boolValue] animated:[self.bottomTabs.animateHide boolValue]];
  167. }
  168. }
  169. if (self.statusBarBlur) {
  170. UIView* curBlurView = [viewController.view viewWithTag:BLUR_STATUS_TAG];
  171. if ([self.statusBarBlur boolValue]) {
  172. if (!curBlurView) {
  173. UIVisualEffectView *blur = [[UIVisualEffectView alloc] initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleLight]];
  174. blur.frame = [[UIApplication sharedApplication] statusBarFrame];
  175. blur.tag = BLUR_STATUS_TAG;
  176. [viewController.view insertSubview:blur atIndex:0];
  177. }
  178. } else {
  179. if (curBlurView) {
  180. [curBlurView removeFromSuperview];
  181. }
  182. }
  183. }
  184. }
  185. - (UIInterfaceOrientationMask)supportedOrientations {
  186. NSArray* orientationsArray = [self.orientation isKindOfClass:[NSString class]] ? @[self.orientation] : self.orientation;
  187. NSUInteger supportedOrientationsMask = 0;
  188. if (!orientationsArray || [self.orientation isEqual:@"default"]) {
  189. return [[UIApplication sharedApplication] supportedInterfaceOrientationsForWindow:[[UIApplication sharedApplication] keyWindow]];
  190. } else {
  191. for (NSString* orientation in orientationsArray) {
  192. if ([orientation isEqualToString:@"all"]) {
  193. supportedOrientationsMask = UIInterfaceOrientationMaskAll;
  194. break;
  195. }
  196. if ([orientation isEqualToString:@"landscape"]) {
  197. supportedOrientationsMask = (supportedOrientationsMask | UIInterfaceOrientationMaskLandscape);
  198. }
  199. if ([orientation isEqualToString:@"portrait"]) {
  200. supportedOrientationsMask = (supportedOrientationsMask | UIInterfaceOrientationMaskPortrait);
  201. }
  202. if ([orientation isEqualToString:@"upsideDown"]) {
  203. supportedOrientationsMask = (supportedOrientationsMask | UIInterfaceOrientationMaskPortraitUpsideDown);
  204. }
  205. }
  206. }
  207. return supportedOrientationsMask;
  208. }
  209. -(void)storeOriginalTopBarImages:(UIViewController*)viewController {
  210. NSMutableDictionary *originalTopBarImages = [@{} mutableCopy];
  211. UIImage *bgImage = [viewController.navigationController.navigationBar backgroundImageForBarMetrics:UIBarMetricsDefault];
  212. if (bgImage != nil) {
  213. originalTopBarImages[@"backgroundImage"] = bgImage;
  214. }
  215. UIImage *shadowImage = viewController.navigationController.navigationBar.shadowImage;
  216. if (shadowImage != nil) {
  217. originalTopBarImages[@"shadowImage"] = shadowImage;
  218. }
  219. self.originalTopBarImages = originalTopBarImages;
  220. }
  221. @end