react-native-navigation的迁移库

UINavigationBar+utils.m 4.1KB

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