react-native-navigation的迁移库

UINavigationBar+utils.m 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. #import "UINavigationBar+utils.h"
  2. #import "RNNFontAttributesCreator.h"
  3. @implementation UINavigationBar (utils)
  4. - (void)rnn_setBackIndicatorImage:(UIImage *)image {
  5. if (@available(iOS 13.0, *)) {
  6. [[self getNavigaitonBarStandardAppearance] setBackIndicatorImage:image transitionMaskImage:image];
  7. [[self getNavigaitonBarCompactAppearance] setBackIndicatorImage:image transitionMaskImage:image];
  8. [[self getNavigaitonBarScrollEdgeAppearance] setBackIndicatorImage:image transitionMaskImage:image];
  9. } else {
  10. [self setBackIndicatorImage:image];
  11. [self setBackIndicatorTransitionMaskImage:image];
  12. }
  13. }
  14. - (void)rnn_setBackgroundColor:(UIColor *)color {
  15. CGFloat bgColorAlpha = CGColorGetAlpha(color.CGColor);
  16. if (color && bgColorAlpha == 0.0) {
  17. self.translucent = YES;
  18. [self setBackgroundColorTransparent];
  19. } else {
  20. self.translucent = NO;
  21. [self setBackgroundColor:color];
  22. }
  23. }
  24. - (void)rnn_setNavigationBarLargeTitleFontFamily:(NSString *)fontFamily fontSize:(NSNumber *)fontSize fontWeight:(NSString *)fontWeight color:(UIColor *)color {
  25. NSDictionary* fontAttributes;
  26. if (@available(iOS 13.0, *)) {
  27. fontAttributes = [RNNFontAttributesCreator createFromDictionary:self.standardAppearance.largeTitleTextAttributes fontFamily:fontFamily fontSize:fontSize defaultFontSize:nil fontWeight:fontWeight color:color defaultColor:nil];
  28. [[self getNavigaitonBarStandardAppearance] setLargeTitleTextAttributes:fontAttributes];
  29. [[self getNavigaitonBarCompactAppearance] setLargeTitleTextAttributes:fontAttributes];
  30. [[self getNavigaitonBarScrollEdgeAppearance] setLargeTitleTextAttributes:fontAttributes];
  31. } else if (@available(iOS 11.0, *)) {
  32. fontAttributes = [RNNFontAttributesCreator createFromDictionary:self.largeTitleTextAttributes fontFamily:fontFamily fontSize:fontSize defaultFontSize:nil fontWeight:fontWeight color:color defaultColor:nil];
  33. self.largeTitleTextAttributes = fontAttributes;
  34. }
  35. }
  36. - (void)rnn_setNavigationBarTitleFontFamily:(NSString *)fontFamily fontSize:(NSNumber *)fontSize fontWeight:(NSString *)fontWeight color:(UIColor *)color {
  37. NSDictionary* fontAttributes;
  38. if (@available(iOS 13.0, *)) {
  39. fontAttributes = [RNNFontAttributesCreator createFromDictionary:self.standardAppearance.titleTextAttributes fontFamily:fontFamily fontSize:fontSize defaultFontSize:nil fontWeight:fontWeight color:color defaultColor:nil];
  40. [[self getNavigaitonBarStandardAppearance] setTitleTextAttributes:fontAttributes];
  41. [[self getNavigaitonBarCompactAppearance] setTitleTextAttributes:fontAttributes];
  42. [[self getNavigaitonBarScrollEdgeAppearance] setTitleTextAttributes:fontAttributes];
  43. } else if (@available(iOS 11.0, *)) {
  44. fontAttributes = [RNNFontAttributesCreator createFromDictionary:self.titleTextAttributes fontFamily:fontFamily fontSize:fontSize defaultFontSize:nil fontWeight:fontWeight color:color defaultColor:nil];
  45. self.titleTextAttributes = fontAttributes;
  46. }
  47. }
  48. - (void)rnn_showBorder:(BOOL)showBorder {
  49. if (@available(iOS 13.0, *)) {
  50. UIColor* shadowColor = showBorder ? [[UINavigationBarAppearance new] shadowColor] : nil;
  51. [[self getNavigaitonBarStandardAppearance] setShadowColor:shadowColor];
  52. [[self getNavigaitonBarCompactAppearance] setShadowColor:shadowColor];
  53. [[self getNavigaitonBarScrollEdgeAppearance] setShadowColor:shadowColor];
  54. } else {
  55. [self setShadowImage:showBorder ? nil : [UIImage new]];
  56. }
  57. }
  58. - (void)setBackgroundColor:(UIColor *)color {
  59. if (@available(iOS 13.0, *)) {
  60. [self getNavigaitonBarStandardAppearance].backgroundColor = color;
  61. [self getNavigaitonBarCompactAppearance].backgroundColor = color;
  62. [self getNavigaitonBarScrollEdgeAppearance].backgroundColor = color;
  63. } else {
  64. [super setBackgroundColor:color];
  65. self.barTintColor = color;
  66. }
  67. }
  68. - (void)setBackgroundColorTransparent {
  69. if (@available(iOS 13.0, *)) {
  70. UIColor* clearColor = [UIColor clearColor];
  71. [self getNavigaitonBarStandardAppearance].backgroundColor = clearColor;
  72. [self getNavigaitonBarCompactAppearance].backgroundColor = clearColor;
  73. [self getNavigaitonBarScrollEdgeAppearance].backgroundColor = clearColor;
  74. [self getNavigaitonBarStandardAppearance].backgroundEffect = nil;
  75. [self getNavigaitonBarCompactAppearance].backgroundEffect = nil;
  76. [self getNavigaitonBarScrollEdgeAppearance].backgroundEffect = nil;
  77. } else {
  78. [self setBackgroundColor:[UIColor clearColor]];
  79. self.shadowImage = [UIImage new];
  80. [self setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
  81. }
  82. }
  83. - (void)configureWithTransparentBackground {
  84. if (@available(iOS 13.0, *)) {
  85. [[self getNavigaitonBarStandardAppearance] configureWithTransparentBackground];
  86. [[self getNavigaitonBarCompactAppearance] configureWithTransparentBackground];
  87. [[self getNavigaitonBarScrollEdgeAppearance] configureWithTransparentBackground];
  88. }
  89. }
  90. - (void)configureWithDefaultBackground {
  91. if (@available(iOS 13.0, *)) {
  92. [[self getNavigaitonBarStandardAppearance] configureWithDefaultBackground];
  93. [[self getNavigaitonBarCompactAppearance] configureWithDefaultBackground];
  94. [[self getNavigaitonBarScrollEdgeAppearance] configureWithDefaultBackground];
  95. }
  96. }
  97. - (UINavigationBarAppearance*)getNavigaitonBarStandardAppearance API_AVAILABLE(ios(13.0)) {
  98. if (!self.standardAppearance) {
  99. self.standardAppearance = [UINavigationBarAppearance new];
  100. }
  101. return self.standardAppearance;
  102. }
  103. - (UINavigationBarAppearance*)getNavigaitonBarCompactAppearance API_AVAILABLE(ios(13.0)) {
  104. if (!self.compactAppearance) {
  105. self.compactAppearance = [UINavigationBarAppearance new];
  106. }
  107. return self.compactAppearance;
  108. }
  109. - (UINavigationBarAppearance*)getNavigaitonBarScrollEdgeAppearance API_AVAILABLE(ios(13.0)) {
  110. if (!self.scrollEdgeAppearance) {
  111. self.scrollEdgeAppearance = [UINavigationBarAppearance new];
  112. }
  113. return self.scrollEdgeAppearance;
  114. }
  115. @end