react-native-navigation的迁移库

UINavigationBar+utils.m 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. self.barTintColor = color;
  65. }
  66. }
  67. - (void)setBackgroundColorTransparent {
  68. if (@available(iOS 13.0, *)) {
  69. UIColor* clearColor = [UIColor clearColor];
  70. [self getNavigaitonBarStandardAppearance].backgroundColor = clearColor;
  71. [self getNavigaitonBarCompactAppearance].backgroundColor = clearColor;
  72. [self getNavigaitonBarScrollEdgeAppearance].backgroundColor = clearColor;
  73. [self getNavigaitonBarStandardAppearance].backgroundEffect = nil;
  74. [self getNavigaitonBarCompactAppearance].backgroundEffect = nil;
  75. [self getNavigaitonBarScrollEdgeAppearance].backgroundEffect = nil;
  76. } else {
  77. self.barTintColor = UIColor.clearColor;
  78. self.shadowImage = [UIImage new];
  79. [self setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
  80. }
  81. }
  82. - (void)configureWithTransparentBackground {
  83. if (@available(iOS 13.0, *)) {
  84. [[self getNavigaitonBarStandardAppearance] configureWithTransparentBackground];
  85. [[self getNavigaitonBarCompactAppearance] configureWithTransparentBackground];
  86. [[self getNavigaitonBarScrollEdgeAppearance] configureWithTransparentBackground];
  87. }
  88. }
  89. - (void)configureWithDefaultBackground {
  90. if (@available(iOS 13.0, *)) {
  91. [[self getNavigaitonBarStandardAppearance] configureWithDefaultBackground];
  92. [[self getNavigaitonBarCompactAppearance] configureWithDefaultBackground];
  93. [[self getNavigaitonBarScrollEdgeAppearance] configureWithDefaultBackground];
  94. }
  95. }
  96. - (UINavigationBarAppearance*)getNavigaitonBarStandardAppearance API_AVAILABLE(ios(13.0)) {
  97. if (!self.standardAppearance) {
  98. self.standardAppearance = [UINavigationBarAppearance new];
  99. }
  100. return self.standardAppearance;
  101. }
  102. - (UINavigationBarAppearance*)getNavigaitonBarCompactAppearance API_AVAILABLE(ios(13.0)) {
  103. if (!self.compactAppearance) {
  104. self.compactAppearance = [UINavigationBarAppearance new];
  105. }
  106. return self.compactAppearance;
  107. }
  108. - (UINavigationBarAppearance*)getNavigaitonBarScrollEdgeAppearance API_AVAILABLE(ios(13.0)) {
  109. if (!self.scrollEdgeAppearance) {
  110. self.scrollEdgeAppearance = [UINavigationBarAppearance new];
  111. }
  112. return self.scrollEdgeAppearance;
  113. }
  114. @end