react-native-navigation的迁移库

RNNStyler.m 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. //
  2. // RNNStyler.m
  3. // ReactNativeNavigation
  4. //
  5. // Created by Ran Greenberg on 24/12/2016.
  6. // Copyright © 2016 artal. All rights reserved.
  7. //
  8. #import "RNNStyler.h"
  9. #import "RCTConvert.h"
  10. @interface RNNStyler()
  11. @property (nonatomic, readwrite) BOOL _hidesBottomBarWhenPushed;
  12. @property (nonatomic, readwrite) BOOL _statusBarHideWithNavBar;
  13. @property (nonatomic, readwrite) BOOL _statusBarHidden;
  14. @property (nonatomic, readwrite) BOOL _statusBarTextColorSchemeLight;
  15. @end
  16. @implementation RNNStyler
  17. #pragma mark - API Methods
  18. -(void)setStyleOnInit:(UIViewController*)viewController styleParams:(NSDictionary*)styleParams {
  19. NSNumber *tabBarHidden = styleParams[STYLE_TAB_BAR_HIDDEN];
  20. self._hidesBottomBarWhenPushed = tabBarHidden ? [tabBarHidden boolValue] : NO;
  21. NSNumber *statusBarHideWithNavBar = styleParams[STYLE_STATUS_BAR_HIDE_WITH_NAV_BAR];
  22. self._statusBarHideWithNavBar = statusBarHideWithNavBar ? [statusBarHideWithNavBar boolValue] : NO;
  23. NSNumber *statusBarHidden = styleParams[STYLE_STATUS_BAR_HIDDEN];
  24. self._statusBarHidden = statusBarHidden ? [statusBarHidden boolValue] : NO;
  25. }
  26. -(void)setStyleOnAppear:(UIViewController*)vc styleParams:(NSDictionary*)styleParams {
  27. [self setNavBarStyle:vc styleParams:styleParams];
  28. [self setStyleOnAppear:vc styleParams:styleParams];
  29. NSString *screenBackgroundColor = styleParams[STYLE_SCREEN_BACKGROUD_COLOR];
  30. if (screenBackgroundColor) {
  31. vc.view.backgroundColor = [RNNStyler colorForProcessedColor:screenBackgroundColor];
  32. }
  33. NSString *statusBarTextColorScheme = styleParams[STYLE_STATUS_BAR_TEXT_COLOR_SCHEME];
  34. if (statusBarTextColorScheme && [statusBarTextColorScheme isEqualToString:STYLE_STATUS_BAR_COLOR_SCHEME_LIGHT]) {
  35. [RNNStyler navBarForVC:vc].barStyle = UIBarStyleBlack;
  36. self._statusBarTextColorSchemeLight = YES;
  37. }
  38. else {
  39. [RNNStyler navBarForVC:vc].barStyle = UIBarStyleDefault;
  40. self._statusBarTextColorSchemeLight = NO;
  41. }
  42. }
  43. #pragma mark - Setters
  44. -(void)setNavBarStyle:(UIViewController*)vc styleParams:(NSDictionary*)styleParams {
  45. NSString *navBarBackgroundColor = styleParams[STYLE_NAV_BAR_BACKGROUND_COLOR];
  46. if (navBarBackgroundColor) {
  47. [RNNStyler navBarForVC:vc].barTintColor = [RNNStyler colorForProcessedColor:navBarBackgroundColor];
  48. }
  49. else {
  50. [RNNStyler navBarForVC:vc].barTintColor = nil;
  51. }
  52. NSString *navBarTextColor = styleParams[STYLE_NAV_BAR_TEXT_COLOR];
  53. if (navBarTextColor) {
  54. [[RNNStyler navBarForVC:vc] setTitleTextAttributes:@{NSForegroundColorAttributeName : [RNNStyler colorForProcessedColor:navBarTextColor]}];
  55. }
  56. else {
  57. [[RNNStyler navBarForVC:vc] setTitleTextAttributes:nil];
  58. }
  59. NSString *navBarButtonColor = styleParams[STYLE_NAV_BAR_BOTTON_COLOR];
  60. if (navBarButtonColor) {
  61. [RNNStyler navBarForVC:vc].tintColor = [RNNStyler colorForProcessedColor:navBarButtonColor];
  62. }
  63. else {
  64. [RNNStyler navBarForVC:vc].tintColor = nil;
  65. }
  66. BOOL navBarHiddenBool = styleParams[STYLE_NAV_BAR_HIDDEN] ? [styleParams[STYLE_NAV_BAR_HIDDEN] boolValue] : NO;
  67. if (vc.navigationController.navigationBarHidden != navBarHiddenBool) {
  68. [vc.navigationController setNavigationBarHidden:navBarHiddenBool animated:YES];
  69. }
  70. BOOL navBarHideOnScrollBool = styleParams[STYLE_NAV_BAR_HIDE_ON_SCROLL] ? [styleParams[STYLE_NAV_BAR_HIDE_ON_SCROLL] boolValue] : NO;
  71. if (navBarHideOnScrollBool) {
  72. vc.navigationController.hidesBarsOnSwipe = YES;
  73. }
  74. else {
  75. vc.navigationController.hidesBarsOnSwipe = NO;
  76. }
  77. BOOL drawUnderNavBarBool = styleParams[STYLE_DRAW_UNDER_NAV_BAR] ? [styleParams[STYLE_DRAW_UNDER_NAV_BAR] boolValue] : NO;
  78. if (drawUnderNavBarBool) {
  79. vc.edgesForExtendedLayout |= UIRectEdgeTop;
  80. }
  81. else {
  82. vc.edgesForExtendedLayout &= ~UIRectEdgeTop;
  83. }
  84. BOOL navBarBlurBool = styleParams[STYLE_NAV_BAR_BLUR] ? [styleParams[STYLE_NAV_BAR_BLUR] boolValue] : NO;
  85. BOOL navBarTranslucentBool = styleParams[STYLE_NAV_BAR_TRANSLUCENT] ? [styleParams[STYLE_NAV_BAR_TRANSLUCENT] boolValue] : NO;
  86. if (navBarTranslucentBool || navBarBlurBool) {
  87. [RNNStyler navBarForVC:vc].translucent = YES;
  88. }
  89. else {
  90. [RNNStyler navBarForVC:vc].translucent = NO;
  91. }
  92. }
  93. -(void)setTabBarStyle:(UIViewController*)vc styleParams:(NSDictionary*)styleParams {
  94. NSNumber *drawUnderTabBar = styleParams[STYLE_DRAW_UNDER_TAB_BAR];
  95. BOOL drawUnderTabBarBool = drawUnderTabBar ? [drawUnderTabBar boolValue] : NO;
  96. if (drawUnderTabBarBool) {
  97. vc.edgesForExtendedLayout |= UIRectEdgeBottom;
  98. }
  99. else {
  100. vc.edgesForExtendedLayout &= ~UIRectEdgeBottom;
  101. }
  102. }
  103. #pragma mark - Helper Static Methods
  104. +(UIColor*)colorForProcessedColor:(NSString*)processedColor {
  105. return processedColor != (id)[NSNull null] ? [RCTConvert UIColor:processedColor] : nil;
  106. }
  107. +(UINavigationBar*)navBarForVC:(UIViewController*)vc {
  108. return vc ? vc.navigationController.navigationBar : nil;
  109. }
  110. -(NSDictionary*)storeOriginalNavBarImages:(UIViewController*)vc {
  111. NSMutableDictionary *originalNavBarImages = [@{} mutableCopy];
  112. UIImage *bgImage = [[RNNStyler navBarForVC:vc] backgroundImageForBarMetrics:UIBarMetricsDefault];
  113. if (bgImage != nil) {
  114. originalNavBarImages[STYLE_NAV_BAR_BACKGROUND_COLOR] = bgImage;
  115. }
  116. UIImage *shadowImage = [RNNStyler navBarForVC:vc].shadowImage;
  117. if (shadowImage != nil) {
  118. originalNavBarImages[STYLE_NAV_BAR_SHADOW_IMAGE] = shadowImage;
  119. }
  120. return originalNavBarImages;
  121. }
  122. @end