react-native-navigation的迁移库

UINavigationController+RNNOptions.m 4.6KB

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