react-native-navigation的迁移库

UINavigationController+RNNOptions.m 4.4KB

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