react-native-navigation的迁移库

UINavigationController+RNNOptions.m 4.5KB

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