react-native-navigation的迁移库

RNNNavigationOptions.m 12KB

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