react-native-navigation的迁移库

RNNNavigationOptions.m 4.4KB

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