react-native-navigation的迁移库

UINavigationController+RNNOptions.m 4.3KB

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