react-native-navigation的迁移库

RNNNavigationOptions.m 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. #import "RNNNavigationOptions.h"
  2. #import <React/RCTConvert.h>
  3. const NSInteger BLUR_STATUS_TAG = 78264801;
  4. @implementation RNNNavigationOptions
  5. -(instancetype)init {
  6. return [self initWithDict:@{}];
  7. }
  8. -(instancetype)initWithDict:(NSDictionary *)navigationOptions {
  9. self = [super init];
  10. self.topBarBackgroundColor = [navigationOptions objectForKey:@"topBarBackgroundColor"];
  11. self.statusBarHidden = [navigationOptions objectForKey:@"statusBarHidden"];
  12. self.title = [navigationOptions objectForKey:@"title"];
  13. self.topBarTextColor = [navigationOptions objectForKey:@"topBarTextColor"];
  14. self.screenBackgroundColor = [navigationOptions objectForKey:@"screenBackgroundColor"];
  15. self.topBarTextFontFamily = [navigationOptions objectForKey:@"topBarTextFontFamily"];
  16. self.topBarHidden = [navigationOptions objectForKey:@"topBarHidden"];
  17. self.topBarHideOnScroll = [navigationOptions objectForKey:@"topBarHideOnScroll"];
  18. self.topBarButtonColor = [navigationOptions objectForKey:@"topBarButtonColor"];
  19. self.topBarTranslucent = [navigationOptions objectForKey:@"topBarTranslucent"];
  20. self.tabBadge = [navigationOptions objectForKey:@"tabBadge"];
  21. self.topBarTextFontSize = [navigationOptions objectForKey:@"topBarTextFontSize"];
  22. self.topBarNoBorder = [navigationOptions objectForKey:@"topBarNoBorder"];
  23. return self;
  24. }
  25. -(void)mergeWith:(NSDictionary *)otherOptions {
  26. for (id key in otherOptions) {
  27. [self setValue:[otherOptions objectForKey:key] forKey:key];
  28. }
  29. }
  30. -(void)applyOn:(UIViewController*)viewController {
  31. if (self.topBarBackgroundColor) {
  32. UIColor* backgroundColor = [RCTConvert UIColor:self.topBarBackgroundColor];
  33. viewController.navigationController.navigationBar.barTintColor = backgroundColor;
  34. } else {
  35. viewController.navigationController.navigationBar.barTintColor = nil;
  36. }
  37. if (self.title) {
  38. viewController.navigationItem.title = self.title;
  39. }
  40. if (self.topBarTextFontFamily || self.topBarTextColor || self.topBarTextFontSize){
  41. NSMutableDictionary* navigationBarTitleTextAttributes = [NSMutableDictionary new];
  42. if (self.topBarTextColor) {
  43. navigationBarTitleTextAttributes[NSForegroundColorAttributeName] = [RCTConvert UIColor:self.topBarTextColor];
  44. }
  45. if (self.topBarTextFontFamily){
  46. if(self.topBarTextFontSize) {
  47. navigationBarTitleTextAttributes[NSFontAttributeName] = [UIFont fontWithName:self.topBarTextFontFamily size:[self.topBarTextFontSize floatValue]];
  48. } else {
  49. navigationBarTitleTextAttributes[NSFontAttributeName] = [UIFont fontWithName:self.topBarTextFontFamily size:20];
  50. }
  51. } else if (self.topBarTextFontSize) {
  52. navigationBarTitleTextAttributes[NSFontAttributeName] = [UIFont systemFontOfSize:[self.topBarTextFontSize floatValue]];
  53. }
  54. viewController.navigationController.navigationBar.titleTextAttributes = navigationBarTitleTextAttributes;
  55. }
  56. if (self.screenBackgroundColor) {
  57. UIColor* screenColor = [RCTConvert UIColor:self.screenBackgroundColor];
  58. viewController.view.backgroundColor = screenColor;
  59. }
  60. if (self.topBarHidden){
  61. if ([self.topBarHidden boolValue]) {
  62. [viewController.navigationController setNavigationBarHidden:YES animated:YES];
  63. } else {
  64. [viewController.navigationController setNavigationBarHidden:NO animated:YES];
  65. }
  66. }
  67. if (self.topBarHideOnScroll) {
  68. BOOL topBarHideOnScrollBool = [self.topBarHideOnScroll boolValue];
  69. if (topBarHideOnScrollBool) {
  70. viewController.navigationController.hidesBarsOnSwipe = YES;
  71. } else {
  72. viewController.navigationController.hidesBarsOnSwipe = NO;
  73. }
  74. }
  75. if (self.topBarButtonColor) {
  76. UIColor* buttonColor = [RCTConvert UIColor:self.topBarButtonColor];
  77. viewController.navigationController.navigationBar.tintColor = buttonColor;
  78. } else {
  79. viewController.navigationController.navigationBar.tintColor = nil;
  80. }
  81. if (self.tabBadge) {
  82. NSString *badge = [RCTConvert NSString:self.tabBadge];
  83. if (viewController.navigationController) {
  84. viewController.navigationController.tabBarItem.badgeValue = badge;
  85. } else {
  86. viewController.tabBarItem.badgeValue = badge;
  87. }
  88. }
  89. if (self.topBarTranslucent) {
  90. if ([self.topBarTranslucent boolValue]) {
  91. viewController.navigationController.navigationBar.translucent = YES;
  92. } else {
  93. viewController.navigationController.navigationBar.translucent = NO;
  94. }
  95. }
  96. if (self.topBarNoBorder) {
  97. if ([self.topBarNoBorder boolValue]) {
  98. viewController.navigationController.navigationBar
  99. .shadowImage = [[UIImage alloc] init];
  100. } else {
  101. viewController.navigationController.navigationBar
  102. .shadowImage = nil;
  103. }
  104. }
  105. if (self.statusBarBlur) {
  106. UIView* curBlurView = [viewController.view viewWithTag:BLUR_STATUS_TAG];
  107. if ([self.statusBarBlur boolValue]) {
  108. if (!curBlurView) {
  109. UIVisualEffectView *blur = [[UIVisualEffectView alloc] initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleLight]];
  110. blur.frame = [[UIApplication sharedApplication] statusBarFrame];
  111. blur.tag = BLUR_STATUS_TAG;
  112. [viewController.view insertSubview:blur atIndex:0];
  113. }
  114. } else {
  115. if (curBlurView) {
  116. [curBlurView removeFromSuperview];
  117. }
  118. }
  119. }
  120. }
  121. @end