react-native-navigation的迁移库

RNNBackgroundOptions.m 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #import "RNNBackgroundOptions.h"
  2. extern const NSInteger BLUR_TOPBAR_TAG;
  3. const NSInteger TOP_BAR_TRANSPARENT_TAG = 78264803;
  4. @interface RNNBackgroundOptions()
  5. @property (nonatomic, strong) NSMutableDictionary* originalTopBarImages;
  6. @end
  7. @implementation RNNBackgroundOptions
  8. - (void)applyOnNavigationController:(UINavigationController *)navigationController {
  9. if (self.translucent) {
  10. navigationController.navigationBar.translucent = [self.translucent boolValue];
  11. } else {
  12. navigationController.navigationBar.translucent = NO;
  13. }
  14. if ([self.blur boolValue]) {
  15. if (![navigationController.navigationBar viewWithTag:BLUR_TOPBAR_TAG]) {
  16. [navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
  17. navigationController.navigationBar.shadowImage = [UIImage new];
  18. UIVisualEffectView *blur = [[UIVisualEffectView alloc] initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleLight]];
  19. CGRect statusBarFrame = [[UIApplication sharedApplication] statusBarFrame];
  20. blur.frame = CGRectMake(0, -1 * statusBarFrame.size.height, navigationController.navigationBar.frame.size.width, navigationController.navigationBar.frame.size.height + statusBarFrame.size.height);
  21. blur.userInteractionEnabled = NO;
  22. blur.tag = BLUR_TOPBAR_TAG;
  23. [navigationController.navigationBar insertSubview:blur atIndex:0];
  24. [navigationController.navigationBar sendSubviewToBack:blur];
  25. }
  26. } else {
  27. UIView *blur = [navigationController.navigationBar viewWithTag:BLUR_TOPBAR_TAG];
  28. if (blur) {
  29. [navigationController.navigationBar setBackgroundImage: nil forBarMetrics:UIBarMetricsDefault];
  30. navigationController.navigationBar.shadowImage = nil;
  31. [blur removeFromSuperview];
  32. }
  33. }
  34. if (self.color && ![self.color isKindOfClass:[NSNull class]]) {
  35. UIColor* backgroundColor = [RCTConvert UIColor:self.color];
  36. CGFloat bgColorAlpha = CGColorGetAlpha(backgroundColor.CGColor);
  37. if (bgColorAlpha == 0.0) {
  38. if (![navigationController.navigationBar viewWithTag:TOP_BAR_TRANSPARENT_TAG]){
  39. [self storeOriginalTopBarImages:navigationController];
  40. UIView *transparentView = [[UIView alloc] initWithFrame:CGRectZero];
  41. transparentView.backgroundColor = [UIColor clearColor];
  42. transparentView.tag = TOP_BAR_TRANSPARENT_TAG;
  43. [navigationController.navigationBar insertSubview:transparentView atIndex:0];
  44. }
  45. navigationController.navigationBar.translucent = YES;
  46. [navigationController.navigationBar setBackgroundColor:[UIColor clearColor]];
  47. navigationController.navigationBar.shadowImage = [UIImage new];
  48. [navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
  49. } else {
  50. navigationController.navigationBar.barTintColor = backgroundColor;
  51. UIView *transparentView = [navigationController.navigationBar viewWithTag:TOP_BAR_TRANSPARENT_TAG];
  52. if (transparentView){
  53. [transparentView removeFromSuperview];
  54. [navigationController.navigationBar setBackgroundImage:self.originalTopBarImages[@"backgroundImage"] forBarMetrics:UIBarMetricsDefault];
  55. navigationController.navigationBar.shadowImage = self.originalTopBarImages[@"shadowImage"];
  56. self.originalTopBarImages = nil;
  57. }
  58. }
  59. } else {
  60. UIView *transparentView = [navigationController.navigationBar viewWithTag:TOP_BAR_TRANSPARENT_TAG];
  61. if (transparentView){
  62. [transparentView removeFromSuperview];
  63. [navigationController.navigationBar setBackgroundImage:self.originalTopBarImages[@"backgroundImage"] forBarMetrics:UIBarMetricsDefault];
  64. navigationController.navigationBar.shadowImage = self.originalTopBarImages[@"shadowImage"];
  65. self.originalTopBarImages = nil;
  66. }
  67. }
  68. if (self.clipToBounds) {
  69. navigationController.navigationBar.clipsToBounds = [self.clipToBounds boolValue];
  70. } else {
  71. navigationController.navigationBar.clipsToBounds = NO;
  72. }
  73. }
  74. - (void)storeOriginalTopBarImages:(UINavigationController *)navigationController {
  75. NSMutableDictionary *originalTopBarImages = [@{} mutableCopy];
  76. UIImage *bgImage = [navigationController.navigationBar backgroundImageForBarMetrics:UIBarMetricsDefault];
  77. if (bgImage != nil) {
  78. originalTopBarImages[@"backgroundImage"] = bgImage;
  79. }
  80. UIImage *shadowImage = navigationController.navigationBar.shadowImage;
  81. if (shadowImage != nil) {
  82. originalTopBarImages[@"shadowImage"] = shadowImage;
  83. }
  84. self.originalTopBarImages = originalTopBarImages;
  85. }
  86. @end