react-native-navigation的迁移库

UINavigationController+RNNOptions.m 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #import "UINavigationController+RNNOptions.h"
  2. #import "RNNFontAttributesCreator.h"
  3. const NSInteger BLUR_TOPBAR_TAG = 78264802;
  4. @implementation UINavigationController (RNNOptions)
  5. - (void)setInteractivePopGestureEnabled:(BOOL)enabled {
  6. self.interactivePopGestureRecognizer.enabled = enabled;
  7. }
  8. - (void)setRootBackgroundImage:(UIImage *)backgroundImage {
  9. UIImageView* backgroundImageView = (self.view.subviews.count > 0) ? self.view.subviews[0] : nil;
  10. if (![backgroundImageView isKindOfClass:[UIImageView class]]) {
  11. backgroundImageView = [[UIImageView alloc] initWithFrame:self.view.bounds];
  12. [self.view insertSubview:backgroundImageView atIndex:0];
  13. }
  14. backgroundImageView.layer.masksToBounds = YES;
  15. backgroundImageView.image = backgroundImage;
  16. [backgroundImageView setContentMode:UIViewContentModeScaleAspectFill];
  17. }
  18. - (void)setNavigationBarTestId:(NSString *)testID {
  19. self.navigationBar.accessibilityIdentifier = testID;
  20. }
  21. - (void)setNavigationBarVisible:(BOOL)visible animated:(BOOL)animated {
  22. [self setNavigationBarHidden:!visible animated:animated];
  23. }
  24. - (void)hideBarsOnScroll:(BOOL)hideOnScroll {
  25. self.hidesBarsOnSwipe = hideOnScroll;
  26. }
  27. - (void)setNavigationBarNoBorder:(BOOL)noBorder {
  28. if (noBorder) {
  29. [self.navigationBar setShadowImage:[[UIImage alloc] init]];
  30. } else {
  31. [self.navigationBar setShadowImage:nil];
  32. }
  33. }
  34. - (void)setBarStyle:(UIBarStyle)barStyle {
  35. self.navigationBar.barStyle = barStyle;
  36. }
  37. - (void)setNavigationBarFontFamily:(NSString *)fontFamily fontSize:(NSNumber *)fontSize fontWeight:(NSString *)fontWeight color:(UIColor *)color {
  38. NSDictionary* fontAttributes = [RNNFontAttributesCreator createWithFontFamily:fontFamily fontSize:fontSize fontWeight:fontWeight color:color];
  39. if (fontAttributes.allKeys.count > 0) {
  40. self.navigationBar.titleTextAttributes = fontAttributes;
  41. }
  42. }
  43. - (void)setNavigationBarLargeTitleVisible:(BOOL)visible {
  44. if (@available(iOS 11.0, *)) {
  45. if (visible){
  46. self.navigationBar.prefersLargeTitles = YES;
  47. } else {
  48. self.navigationBar.prefersLargeTitles = NO;
  49. }
  50. }
  51. }
  52. - (void)setNavigationBarLargeTitleFontFamily:(NSString *)fontFamily fontSize:(NSNumber *)fontSize fontWeight:(NSString *)fontWeight color:(UIColor *)color {
  53. if (@available(iOS 11.0, *)) {
  54. NSDictionary* fontAttributes = [RNNFontAttributesCreator createWithFontFamily:fontFamily fontSize:fontSize fontWeight:fontWeight color:color];
  55. self.navigationBar.largeTitleTextAttributes = fontAttributes;
  56. }
  57. }
  58. - (void)setNavigationBarTranslucent:(BOOL)translucent {
  59. self.navigationBar.translucent = translucent;
  60. }
  61. - (void)setNavigationBarBlur:(BOOL)blur {
  62. if (blur && ![self.navigationBar viewWithTag:BLUR_TOPBAR_TAG]) {
  63. [self.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
  64. self.navigationBar.shadowImage = [UIImage new];
  65. UIVisualEffectView *blur = [[UIVisualEffectView alloc] initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleLight]];
  66. CGRect statusBarFrame = [[UIApplication sharedApplication] statusBarFrame];
  67. blur.frame = CGRectMake(0, -1 * statusBarFrame.size.height, self.navigationBar.frame.size.width, self.navigationBar.frame.size.height + statusBarFrame.size.height);
  68. blur.userInteractionEnabled = NO;
  69. blur.tag = BLUR_TOPBAR_TAG;
  70. [self.navigationBar insertSubview:blur atIndex:0];
  71. [self.navigationBar sendSubviewToBack:blur];
  72. } else {
  73. UIView *blur = [self.navigationBar viewWithTag:BLUR_TOPBAR_TAG];
  74. if (blur) {
  75. [self.navigationBar setBackgroundImage: nil forBarMetrics:UIBarMetricsDefault];
  76. self.navigationBar.shadowImage = nil;
  77. [blur removeFromSuperview];
  78. }
  79. }
  80. }
  81. - (void)setBackButtonColor:(UIColor *)color {
  82. self.navigationBar.tintColor = color;
  83. }
  84. - (void)setNavigationBarClipsToBounds:(BOOL)clipsToBounds {
  85. self.navigationBar.clipsToBounds = clipsToBounds;
  86. }
  87. @end