react-native-navigation的迁移库

UINavigationController+RNNOptions.m 4.6KB

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