react-native-navigation的迁移库

UIViewController+RNNOptions.m 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. #import "UIViewController+RNNOptions.h"
  2. #import <React/RCTRootView.h>
  3. #import "UIImage+tint.h"
  4. #import "UINavigationBar+utils.h"
  5. #import "RNNBottomTabOptions.h"
  6. #import "RNNNavigationOptions.h"
  7. #import "RNNBackButtonOptions.h"
  8. #define kStatusBarAnimationDuration 0.35
  9. const NSInteger BLUR_STATUS_TAG = 78264801;
  10. @implementation UIViewController (RNNOptions)
  11. - (void)setBackgroundImage:(UIImage *)backgroundImage {
  12. if (backgroundImage) {
  13. UIImageView* backgroundImageView = (self.view.subviews.count > 0) ? self.view.subviews[0] : nil;
  14. if (![backgroundImageView isKindOfClass:[UIImageView class]]) {
  15. backgroundImageView = [[UIImageView alloc] initWithFrame:self.view.bounds];
  16. [self.view insertSubview:backgroundImageView atIndex:0];
  17. }
  18. backgroundImageView.layer.masksToBounds = YES;
  19. backgroundImageView.image = backgroundImage;
  20. [backgroundImageView setContentMode:UIViewContentModeScaleAspectFill];
  21. }
  22. }
  23. - (void)setSearchBarWithPlaceholder:(NSString *)placeholder
  24. hideNavBarOnFocusSearchBar:(BOOL)hideNavBarOnFocusSearchBar {
  25. if (@available(iOS 11.0, *)) {
  26. if (!self.navigationItem.searchController) {
  27. UISearchController *search = [[UISearchController alloc]initWithSearchResultsController:nil];
  28. search.dimsBackgroundDuringPresentation = NO;
  29. if ([self conformsToProtocol:@protocol(UISearchResultsUpdating)]) {
  30. [search setSearchResultsUpdater:((UIViewController <UISearchResultsUpdating> *) self)];
  31. }
  32. search.searchBar.delegate = (id<UISearchBarDelegate>)self;
  33. if (placeholder) {
  34. search.searchBar.placeholder = placeholder;
  35. }
  36. search.hidesNavigationBarDuringPresentation = hideNavBarOnFocusSearchBar;
  37. self.navigationItem.searchController = search;
  38. [self.navigationItem setHidesSearchBarWhenScrolling:NO];
  39. // Fixes #3450, otherwise, UIKit will infer the presentation context to be the root most view controller
  40. self.definesPresentationContext = YES;
  41. }
  42. }
  43. }
  44. - (void)setSearchBarHiddenWhenScrolling:(BOOL)searchBarHidden {
  45. if (@available(iOS 11.0, *)) {
  46. self.navigationItem.hidesSearchBarWhenScrolling = searchBarHidden;
  47. }
  48. }
  49. - (void)setNavigationItemTitle:(NSString *)title {
  50. self.navigationItem.title = title;
  51. }
  52. - (void)setDrawBehindTopBar:(BOOL)drawBehind {
  53. if (drawBehind) {
  54. [self setExtendedLayoutIncludesOpaqueBars:YES];
  55. self.edgesForExtendedLayout |= UIRectEdgeTop;
  56. } else {
  57. self.edgesForExtendedLayout &= ~UIRectEdgeTop;
  58. }
  59. }
  60. - (void)setDrawBehindTabBar:(BOOL)drawBehindTabBar {
  61. if (drawBehindTabBar) {
  62. [self setExtendedLayoutIncludesOpaqueBars:YES];
  63. self.edgesForExtendedLayout |= UIRectEdgeBottom;
  64. } else {
  65. self.edgesForExtendedLayout &= ~UIRectEdgeBottom;
  66. }
  67. }
  68. - (void)setTabBarItemBadge:(NSString *)badge {
  69. UITabBarItem *tabBarItem = self.tabBarItem;
  70. if ([badge isKindOfClass:[NSNull class]] || [badge isEqualToString:@""]) {
  71. tabBarItem.badgeValue = nil;
  72. } else {
  73. tabBarItem.badgeValue = badge;
  74. [[self.tabBarController.tabBar viewWithTag:tabBarItem.tag] removeFromSuperview];
  75. tabBarItem.tag = -1;
  76. }
  77. }
  78. - (void)setTabBarItemBadgeColor:(UIColor *)badgeColor {
  79. if (@available(iOS 10.0, *)) {
  80. self.tabBarItem.badgeColor = badgeColor;
  81. }
  82. }
  83. - (void)setStatusBarStyle:(NSString *)style animated:(BOOL)animated {
  84. if (animated) {
  85. [UIView animateWithDuration:[self statusBarAnimationDuration:animated] animations:^{
  86. [self setNeedsStatusBarAppearanceUpdate];
  87. }];
  88. } else {
  89. [self setNeedsStatusBarAppearanceUpdate];
  90. }
  91. }
  92. - (void)setTopBarPrefersLargeTitle:(BOOL)prefersLargeTitle {
  93. if (@available(iOS 11.0, *)) {
  94. if (prefersLargeTitle) {
  95. self.navigationItem.largeTitleDisplayMode = UINavigationItemLargeTitleDisplayModeAlways;
  96. } else {
  97. self.navigationItem.largeTitleDisplayMode = UINavigationItemLargeTitleDisplayModeNever;
  98. }
  99. }
  100. }
  101. - (void)setStatusBarBlur:(BOOL)blur {
  102. UIView* curBlurView = [self.view viewWithTag:BLUR_STATUS_TAG];
  103. if (blur) {
  104. if (!curBlurView) {
  105. UIVisualEffectView *blur = [[UIVisualEffectView alloc] initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleLight]];
  106. blur.frame = [[UIApplication sharedApplication] statusBarFrame];
  107. blur.tag = BLUR_STATUS_TAG;
  108. [self.view insertSubview:blur atIndex:0];
  109. }
  110. } else {
  111. if (curBlurView) {
  112. [curBlurView removeFromSuperview];
  113. }
  114. }
  115. }
  116. - (void)setBackgroundColor:(UIColor *)backgroundColor {
  117. self.view.backgroundColor = backgroundColor;
  118. }
  119. - (void)setBackButtonVisible:(BOOL)visible {
  120. self.navigationItem.hidesBackButton = !visible;
  121. }
  122. - (CGFloat)statusBarAnimationDuration:(BOOL)animated {
  123. return animated ? kStatusBarAnimationDuration : CGFLOAT_MIN;
  124. }
  125. - (BOOL)isModal {
  126. if([self presentingViewController])
  127. return YES;
  128. if([[[self navigationController] presentingViewController] presentedViewController] == [self navigationController])
  129. return YES;
  130. if([[[self tabBarController] presentingViewController] isKindOfClass:[UITabBarController class]])
  131. return YES;
  132. return NO;
  133. }
  134. - (void)setInterceptTouchOutside:(BOOL)interceptTouchOutside {
  135. if ([self.view isKindOfClass:[RCTRootView class]]) {
  136. RCTRootView* rootView = (RCTRootView*)self.view;
  137. rootView.passThroughTouches = !interceptTouchOutside;
  138. }
  139. }
  140. - (void)applyBackButton:(RNNBackButtonOptions *)backButton {
  141. UIBarButtonItem *backItem = [UIBarButtonItem new];
  142. if (backButton.icon.hasValue) {
  143. UIColor *color = [backButton.color getWithDefaultValue:nil];
  144. backItem.image = color ?
  145. [[backButton.icon.get withTintColor:color] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] :
  146. backButton.icon.get;
  147. [self.navigationController.navigationBar setBackIndicatorImage:[UIImage new]];
  148. [self.navigationController.navigationBar setBackIndicatorTransitionMaskImage:[UIImage new]];
  149. }
  150. if ([backButton.showTitle getWithDefaultValue:YES]) backItem.title = [backButton.title getWithDefaultValue:nil];
  151. if (backButton.color.hasValue) backItem.tintColor = [backButton.color get];
  152. self.navigationItem.backBarButtonItem = backItem;
  153. }
  154. @end