react-native-navigation的迁移库

RNNBackgroundOptions.m 4.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. }
  69. - (void)storeOriginalTopBarImages:(UINavigationController *)navigationController {
  70. NSMutableDictionary *originalTopBarImages = [@{} mutableCopy];
  71. UIImage *bgImage = [navigationController.navigationBar backgroundImageForBarMetrics:UIBarMetricsDefault];
  72. if (bgImage != nil) {
  73. originalTopBarImages[@"backgroundImage"] = bgImage;
  74. }
  75. UIImage *shadowImage = navigationController.navigationBar.shadowImage;
  76. if (shadowImage != nil) {
  77. originalTopBarImages[@"shadowImage"] = shadowImage;
  78. }
  79. self.originalTopBarImages = originalTopBarImages;
  80. }
  81. @end