react-native-navigation的迁移库

RNNNavigationOptions.m 13KB

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