react-native-navigation的迁移库

UIViewController+RNNOptions.m 6.7KB

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