react-native-navigation的迁移库

UINavigationController+RNNOptions.m 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. [self.navigationBar rnn_showBorder:!noBorder];
  31. }
  32. - (void)setBarStyle:(UIBarStyle)barStyle {
  33. self.navigationBar.barStyle = barStyle;
  34. }
  35. - (void)setNavigationBarFontFamily:(NSString *)fontFamily fontSize:(NSNumber *)fontSize fontWeight:(NSString *)fontWeight color:(UIColor *)color {
  36. NSDictionary* fontAttributes = [RNNFontAttributesCreator createWithFontFamily:fontFamily fontSize:fontSize fontWeight:fontWeight color:color];
  37. if (fontAttributes.allKeys.count > 0) {
  38. self.navigationBar.titleTextAttributes = fontAttributes;
  39. }
  40. }
  41. - (void)setNavigationBarLargeTitleVisible:(BOOL)visible {
  42. if (@available(iOS 11.0, *)) {
  43. if (visible){
  44. self.navigationBar.prefersLargeTitles = YES;
  45. } else {
  46. self.navigationBar.prefersLargeTitles = NO;
  47. }
  48. }
  49. }
  50. - (void)setNavigationBarLargeTitleFontFamily:(NSString *)fontFamily fontSize:(NSNumber *)fontSize fontWeight:(NSString *)fontWeight color:(UIColor *)color {
  51. if (@available(iOS 11.0, *)) {
  52. NSDictionary* fontAttributes = [RNNFontAttributesCreator createWithFontFamily:fontFamily fontSize:fontSize fontWeight:fontWeight color:color];
  53. self.navigationBar.largeTitleTextAttributes = fontAttributes;
  54. }
  55. }
  56. - (void)setNavigationBarTranslucent:(BOOL)translucent {
  57. self.navigationBar.translucent = translucent;
  58. }
  59. - (void)setNavigationBarBlur:(BOOL)blur {
  60. if (blur && ![self.navigationBar viewWithTag:BLUR_TOPBAR_TAG]) {
  61. [self.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
  62. self.navigationBar.shadowImage = [UIImage new];
  63. UIVisualEffectView *blur = [[UIVisualEffectView alloc] initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleLight]];
  64. CGRect statusBarFrame = [[UIApplication sharedApplication] statusBarFrame];
  65. blur.frame = CGRectMake(0, -1 * statusBarFrame.size.height, self.navigationBar.frame.size.width, self.navigationBar.frame.size.height + statusBarFrame.size.height);
  66. blur.userInteractionEnabled = NO;
  67. blur.tag = BLUR_TOPBAR_TAG;
  68. [self.navigationBar insertSubview:blur atIndex:0];
  69. [self.navigationBar sendSubviewToBack:blur];
  70. } else {
  71. UIView *blur = [self.navigationBar viewWithTag:BLUR_TOPBAR_TAG];
  72. if (blur) {
  73. [self.navigationBar setBackgroundImage: nil forBarMetrics:UIBarMetricsDefault];
  74. self.navigationBar.shadowImage = nil;
  75. [blur removeFromSuperview];
  76. }
  77. }
  78. }
  79. - (void)setBackButtonColor:(UIColor *)color {
  80. self.navigationBar.tintColor = color;
  81. }
  82. - (void)setNavigationBarClipsToBounds:(BOOL)clipsToBounds {
  83. self.navigationBar.clipsToBounds = clipsToBounds;
  84. }
  85. - (void)setBackButtonIcon:(UIImage *)icon withColor:(UIColor *)color title:(NSString *)title showTitle:(BOOL)showTitle {
  86. UIBarButtonItem *backItem = [[UIBarButtonItem alloc] init];
  87. if (icon) {
  88. icon = color
  89. ? [[icon withTintColor:color] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]
  90. : icon;
  91. }
  92. [self.navigationBar rnn_setBackIndicatorImage:icon];
  93. UIViewController *lastViewControllerInStack = self.viewControllers.count > 1 ? self.viewControllers[self.viewControllers.count - 2] : self.topViewController;
  94. if (showTitle) {
  95. backItem.title = title ? title : lastViewControllerInStack.navigationItem.title;
  96. }
  97. backItem.tintColor = color;
  98. lastViewControllerInStack.navigationItem.backBarButtonItem = backItem;
  99. }
  100. @end