react-native-navigation的迁移库

RNNNavigationOptions.m 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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. }
  85. if (self.topBar.hidden){
  86. [viewController.navigationController setNavigationBarHidden:[self.topBar.hidden boolValue] animated:[self.topBar.animateHide boolValue]];
  87. }
  88. if (self.topBar.hideOnScroll) {
  89. viewController.navigationController.hidesBarsOnSwipe = [self.topBar.hideOnScroll boolValue];
  90. }
  91. if (self.topBar.buttonColor) {
  92. UIColor* buttonColor = [RCTConvert UIColor:self.topBar.buttonColor];
  93. viewController.navigationController.navigationBar.tintColor = buttonColor;
  94. } else {
  95. viewController.navigationController.navigationBar.tintColor = nil;
  96. }
  97. if ([self.topBar.blur boolValue]) {
  98. if (![viewController.navigationController.navigationBar viewWithTag:BLUR_TOPBAR_TAG]) {
  99. [viewController.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
  100. viewController.navigationController.navigationBar.shadowImage = [UIImage new];
  101. UIVisualEffectView *blur = [[UIVisualEffectView alloc] initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleLight]];
  102. CGRect statusBarFrame = [[UIApplication sharedApplication] statusBarFrame];
  103. blur.frame = CGRectMake(0, -1 * statusBarFrame.size.height, viewController.navigationController.navigationBar.frame.size.width, viewController.navigationController.navigationBar.frame.size.height + statusBarFrame.size.height);
  104. blur.userInteractionEnabled = NO;
  105. blur.tag = BLUR_TOPBAR_TAG;
  106. [viewController.navigationController.navigationBar insertSubview:blur atIndex:0];
  107. [viewController.navigationController.navigationBar sendSubviewToBack:blur];
  108. }
  109. } else {
  110. UIView *blur = [viewController.navigationController.navigationBar viewWithTag:BLUR_TOPBAR_TAG];
  111. if (blur) {
  112. [viewController.navigationController.navigationBar setBackgroundImage: nil forBarMetrics:UIBarMetricsDefault];
  113. viewController.navigationController.navigationBar.shadowImage = nil;
  114. [blur removeFromSuperview];
  115. }
  116. }
  117. void (^disableTopBarTransparent)() = ^void(){
  118. UIView *transparentView = [viewController.navigationController.navigationBar viewWithTag:TOP_BAR_TRANSPARENT_TAG];
  119. if (transparentView){
  120. [transparentView removeFromSuperview];
  121. [viewController.navigationController.navigationBar setBackgroundImage:self.originalTopBarImages[@"backgroundImage"] forBarMetrics:UIBarMetricsDefault];
  122. viewController.navigationController.navigationBar.shadowImage = self.originalTopBarImages[@"shadowImage"];
  123. self.originalTopBarImages = nil;
  124. }
  125. };
  126. if (self.topBar.transparent) {
  127. if ([self.topBar.transparent boolValue]) {
  128. if (![viewController.navigationController.navigationBar viewWithTag:TOP_BAR_TRANSPARENT_TAG]){
  129. [self storeOriginalTopBarImages:viewController];
  130. [viewController.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
  131. viewController.navigationController.navigationBar.shadowImage = [UIImage new];
  132. UIView *transparentView = [[UIView alloc] initWithFrame:CGRectZero];
  133. transparentView.tag = TOP_BAR_TRANSPARENT_TAG;
  134. [viewController.navigationController.navigationBar insertSubview:transparentView atIndex:0];
  135. }
  136. } else {
  137. disableTopBarTransparent();
  138. }
  139. } else {
  140. disableTopBarTransparent();
  141. }
  142. if (self.topBar.translucent) {
  143. viewController.navigationController.navigationBar.translucent = [self.topBar.translucent boolValue];
  144. }
  145. if (self.topBar.drawUnder) {
  146. if ([self.topBar.drawUnder boolValue]) {
  147. viewController.edgesForExtendedLayout |= UIRectEdgeTop;
  148. } else {
  149. viewController.edgesForExtendedLayout &= ~UIRectEdgeTop;
  150. }
  151. }
  152. if (self.topBar.noBorder) {
  153. if ([self.topBar.noBorder boolValue]) {
  154. viewController.navigationController.navigationBar
  155. .shadowImage = [[UIImage alloc] init];
  156. } else {
  157. viewController.navigationController.navigationBar
  158. .shadowImage = nil;
  159. }
  160. }
  161. if (self.topBar.testID) {
  162. viewController.navigationController.navigationBar.accessibilityIdentifier = self.topBar.testID;
  163. }
  164. }
  165. if (self.popGesture) {
  166. viewController.navigationController.interactivePopGestureRecognizer.enabled = [self.popGesture boolValue];
  167. }
  168. if (self.screenBackgroundColor) {
  169. UIColor* screenColor = [RCTConvert UIColor:self.screenBackgroundColor];
  170. viewController.view.backgroundColor = screenColor;
  171. }
  172. if (self.bottomTabs) {
  173. if (self.bottomTabs.currentTabIndex) {
  174. [viewController.tabBarController setSelectedIndex:[self.bottomTabs.currentTabIndex unsignedIntegerValue]];
  175. }
  176. if (self.bottomTabs.hidden) {
  177. [((RNNTabBarController *)viewController.tabBarController) setTabBarHidden:[self.bottomTabs.hidden boolValue] animated:[self.bottomTabs.animateHide boolValue]];
  178. }
  179. if (self.bottomTabs.testID) {
  180. viewController.tabBarController.tabBar.accessibilityIdentifier = self.bottomTabs.testID;
  181. }
  182. if (self.bottomTabs.drawUnder) {
  183. if ([self.bottomTabs.drawUnder boolValue]) {
  184. viewController.edgesForExtendedLayout |= UIRectEdgeBottom;
  185. } else {
  186. viewController.edgesForExtendedLayout &= ~UIRectEdgeBottom;
  187. }
  188. }
  189. }
  190. if (self.statusBarBlur) {
  191. UIView* curBlurView = [viewController.view viewWithTag:BLUR_STATUS_TAG];
  192. if ([self.statusBarBlur boolValue]) {
  193. if (!curBlurView) {
  194. UIVisualEffectView *blur = [[UIVisualEffectView alloc] initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleLight]];
  195. blur.frame = [[UIApplication sharedApplication] statusBarFrame];
  196. blur.tag = BLUR_STATUS_TAG;
  197. [viewController.view insertSubview:blur atIndex:0];
  198. }
  199. } else {
  200. if (curBlurView) {
  201. [curBlurView removeFromSuperview];
  202. }
  203. }
  204. }
  205. RNNSideMenuController* sideMenuController = (RNNSideMenuController*)UIApplication.sharedApplication.delegate.window.rootViewController;
  206. if ([sideMenuController isKindOfClass:[RNNSideMenuController class]]) {
  207. if (self.sideMenu.leftSideVisible) {
  208. if (self.sideMenu.leftSideVisible.boolValue) {
  209. [sideMenuController showSideMenu:MMDrawerSideLeft animated:YES];
  210. } else {
  211. [sideMenuController hideSideMenu:MMDrawerSideLeft animated:YES];
  212. }
  213. }
  214. if (self.sideMenu.rightSideVisible) {
  215. if (self.sideMenu.rightSideVisible.boolValue) {
  216. [sideMenuController showSideMenu:MMDrawerSideRight animated:YES];
  217. } else {
  218. [sideMenuController hideSideMenu:MMDrawerSideRight animated:YES];
  219. }
  220. }
  221. [self.sideMenu resetOptions];
  222. }
  223. if (self.backgroundImage) {
  224. UIImageView* backgroundImageView = (viewController.view.subviews.count > 0) ? viewController.view.subviews[0] : nil;
  225. if (![backgroundImageView isKindOfClass:[UIImageView class]]) {
  226. backgroundImageView = [[UIImageView alloc] initWithFrame:viewController.view.bounds];
  227. [viewController.view insertSubview:backgroundImageView atIndex:0];
  228. }
  229. backgroundImageView.layer.masksToBounds = YES;
  230. backgroundImageView.image = self.backgroundImage;
  231. [backgroundImageView setContentMode:UIViewContentModeScaleAspectFill];
  232. }
  233. if (self.rootBackgroundImage) {
  234. UIImageView* backgroundImageView = (viewController.navigationController.view.subviews.count > 0) ? viewController.navigationController.view.subviews[0] : nil;
  235. if (![backgroundImageView isKindOfClass:[UIImageView class]]) {
  236. backgroundImageView = [[UIImageView alloc] initWithFrame:viewController.view.bounds];
  237. [viewController.navigationController.view insertSubview:backgroundImageView atIndex:0];
  238. }
  239. backgroundImageView.layer.masksToBounds = YES;
  240. backgroundImageView.image = self.rootBackgroundImage;
  241. [backgroundImageView setContentMode:UIViewContentModeScaleAspectFill];
  242. }
  243. [self applyTabBarItemOptions:viewController];
  244. }
  245. - (void)applyTabBarItemOptions:(UIViewController*)viewController {
  246. if (self.tabItem) {
  247. if (self.tabItem.title || self.tabItem.icon) {
  248. UITabBarItem* tabItem = [[UITabBarItem alloc] initWithTitle:self.tabItem.title image:[RCTConvert UIImage:self.tabItem.icon] tag:self.tabItem.tag];
  249. tabItem.accessibilityIdentifier = self.tabItem.testID;
  250. [viewController.navigationController setTabBarItem:tabItem];
  251. }
  252. if (self.tabItem.badge) {
  253. NSString *badge = [RCTConvert NSString:self.tabItem.badge];
  254. if (viewController.navigationController) {
  255. viewController.navigationController.tabBarItem.badgeValue = badge;
  256. } else {
  257. viewController.tabBarItem.badgeValue = badge;
  258. }
  259. }
  260. if (self.tabItem.visible) {
  261. [viewController.tabBarController setSelectedIndex:[viewController.tabBarController.viewControllers indexOfObject:viewController]];
  262. }
  263. [self.tabItem resetOptions];
  264. }
  265. }
  266. - (UIInterfaceOrientationMask)supportedOrientations {
  267. NSArray* orientationsArray = [self.orientation isKindOfClass:[NSString class]] ? @[self.orientation] : self.orientation;
  268. NSUInteger supportedOrientationsMask = 0;
  269. if (!orientationsArray || [self.orientation isEqual:@"default"]) {
  270. return [[UIApplication sharedApplication] supportedInterfaceOrientationsForWindow:[[UIApplication sharedApplication] keyWindow]];
  271. } else {
  272. for (NSString* orientation in orientationsArray) {
  273. if ([orientation isEqualToString:@"all"]) {
  274. supportedOrientationsMask = UIInterfaceOrientationMaskAll;
  275. break;
  276. }
  277. if ([orientation isEqualToString:@"landscape"]) {
  278. supportedOrientationsMask = (supportedOrientationsMask | UIInterfaceOrientationMaskLandscape);
  279. }
  280. if ([orientation isEqualToString:@"portrait"]) {
  281. supportedOrientationsMask = (supportedOrientationsMask | UIInterfaceOrientationMaskPortrait);
  282. }
  283. if ([orientation isEqualToString:@"upsideDown"]) {
  284. supportedOrientationsMask = (supportedOrientationsMask | UIInterfaceOrientationMaskPortraitUpsideDown);
  285. }
  286. }
  287. }
  288. return supportedOrientationsMask;
  289. }
  290. -(void)storeOriginalTopBarImages:(UIViewController*)viewController {
  291. NSMutableDictionary *originalTopBarImages = [@{} mutableCopy];
  292. UIImage *bgImage = [viewController.navigationController.navigationBar backgroundImageForBarMetrics:UIBarMetricsDefault];
  293. if (bgImage != nil) {
  294. originalTopBarImages[@"backgroundImage"] = bgImage;
  295. }
  296. UIImage *shadowImage = viewController.navigationController.navigationBar.shadowImage;
  297. if (shadowImage != nil) {
  298. originalTopBarImages[@"shadowImage"] = shadowImage;
  299. }
  300. self.originalTopBarImages = originalTopBarImages;
  301. }
  302. @end