react-native-navigation的迁移库

UINavigationBar+utils.m 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #import "UINavigationBar+utils.h"
  2. const NSInteger TOP_BAR_TRANSPARENT_TAG = 78264803;
  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)setBackgroundColor:(UIColor *)color {
  25. if (@available(iOS 13.0, *)) {
  26. [self getNavigaitonBarStandardAppearance].backgroundColor = color;
  27. [self getNavigaitonBarCompactAppearance].backgroundColor = color;
  28. [self getNavigaitonBarScrollEdgeAppearance].backgroundColor = color;
  29. } else {
  30. [super setBackgroundColor:color];
  31. [self removeTransparentView];
  32. }
  33. }
  34. - (void)setBackgroundColorTransparent {
  35. if (@available(iOS 13.0, *)) {
  36. UIColor* clearColor = [UIColor clearColor];
  37. [self getNavigaitonBarStandardAppearance].backgroundColor = clearColor;
  38. [self getNavigaitonBarCompactAppearance].backgroundColor = clearColor;
  39. [self getNavigaitonBarScrollEdgeAppearance].backgroundColor = clearColor;
  40. [self getNavigaitonBarStandardAppearance].backgroundEffect = nil;
  41. [self getNavigaitonBarCompactAppearance].backgroundEffect = nil;
  42. [self getNavigaitonBarScrollEdgeAppearance].backgroundEffect = nil;
  43. } else {
  44. if (![self viewWithTag:TOP_BAR_TRANSPARENT_TAG]){
  45. UIView *transparentView = [[UIView alloc] initWithFrame:CGRectZero];
  46. transparentView.backgroundColor = [UIColor clearColor];
  47. transparentView.tag = TOP_BAR_TRANSPARENT_TAG;
  48. [self insertSubview:transparentView atIndex:0];
  49. }
  50. [self setBackgroundColor:[UIColor clearColor]];
  51. self.shadowImage = [UIImage new];
  52. [self setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
  53. }
  54. }
  55. - (void)rnn_showBorder:(BOOL)showBorder {
  56. if (@available(iOS 13.0, *)) {
  57. UIColor* shadowColor = showBorder ? [[UINavigationBarAppearance new] shadowColor] : nil;
  58. [[self getNavigaitonBarStandardAppearance] setShadowColor:shadowColor];
  59. [[self getNavigaitonBarCompactAppearance] setShadowColor:shadowColor];
  60. [[self getNavigaitonBarScrollEdgeAppearance] setShadowColor:shadowColor];
  61. } else {
  62. [self setShadowImage:showBorder ? nil : [UIImage new]];
  63. }
  64. }
  65. - (void)removeTransparentView {
  66. UIView *transparentView = [self viewWithTag:TOP_BAR_TRANSPARENT_TAG];
  67. if (transparentView){
  68. [transparentView removeFromSuperview];
  69. }
  70. }
  71. - (void)configureWithTransparentBackground {
  72. if (@available(iOS 13.0, *)) {
  73. [[self getNavigaitonBarStandardAppearance] configureWithTransparentBackground];
  74. [[self getNavigaitonBarCompactAppearance] configureWithTransparentBackground];
  75. [[self getNavigaitonBarScrollEdgeAppearance] configureWithTransparentBackground];
  76. }
  77. }
  78. - (void)configureWithDefaultBackground {
  79. if (@available(iOS 13.0, *)) {
  80. [[self getNavigaitonBarStandardAppearance] configureWithDefaultBackground];
  81. [[self getNavigaitonBarCompactAppearance] configureWithDefaultBackground];
  82. [[self getNavigaitonBarScrollEdgeAppearance] configureWithDefaultBackground];
  83. }
  84. }
  85. - (UINavigationBarAppearance*)getNavigaitonBarStandardAppearance API_AVAILABLE(ios(13.0)) {
  86. if (!self.standardAppearance) {
  87. self.standardAppearance = [UINavigationBarAppearance new];
  88. }
  89. return self.standardAppearance;
  90. }
  91. - (UINavigationBarAppearance*)getNavigaitonBarCompactAppearance API_AVAILABLE(ios(13.0)) {
  92. if (!self.compactAppearance) {
  93. self.compactAppearance = [UINavigationBarAppearance new];
  94. }
  95. return self.compactAppearance;
  96. }
  97. - (UINavigationBarAppearance*)getNavigaitonBarScrollEdgeAppearance API_AVAILABLE(ios(13.0)) {
  98. if (!self.scrollEdgeAppearance) {
  99. self.scrollEdgeAppearance = [UINavigationBarAppearance new];
  100. }
  101. return self.scrollEdgeAppearance;
  102. }
  103. @end