react-native-navigation的迁移库

RNNNavigationOptions.m 15KB

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