react-native-navigation的迁移库

RNNNavigationOptions.m 15KB

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