react-native-navigation的迁移库

UIViewController+RNNOptions.m 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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)rnn_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)rnn_setModalPresentationStyle:(UIModalPresentationStyle)modalPresentationStyle {
  23. self.modalPresentationStyle = modalPresentationStyle;
  24. }
  25. - (void)rnn_setModalTransitionStyle:(UIModalTransitionStyle)modalTransitionStyle {
  26. self.modalTransitionStyle = modalTransitionStyle;
  27. }
  28. - (void)rnn_setSearchBarWithPlaceholder:(NSString *)placeholder
  29. hideNavBarOnFocusSearchBar:(BOOL)hideNavBarOnFocusSearchBar {
  30. if (@available(iOS 11.0, *)) {
  31. if (!self.navigationItem.searchController) {
  32. UISearchController *search = [[UISearchController alloc]initWithSearchResultsController:nil];
  33. search.dimsBackgroundDuringPresentation = NO;
  34. if ([self conformsToProtocol:@protocol(UISearchResultsUpdating)]) {
  35. [search setSearchResultsUpdater:((UIViewController <UISearchResultsUpdating> *) self)];
  36. }
  37. search.searchBar.delegate = (id<UISearchBarDelegate>)self;
  38. if (placeholder) {
  39. search.searchBar.placeholder = placeholder;
  40. }
  41. search.hidesNavigationBarDuringPresentation = hideNavBarOnFocusSearchBar;
  42. self.navigationItem.searchController = search;
  43. [self.navigationItem setHidesSearchBarWhenScrolling:NO];
  44. // Fixes #3450, otherwise, UIKit will infer the presentation context to be the root most view controller
  45. self.definesPresentationContext = YES;
  46. }
  47. }
  48. }
  49. - (void)rnn_setSearchBarHiddenWhenScrolling:(BOOL)searchBarHidden {
  50. if (@available(iOS 11.0, *)) {
  51. self.navigationItem.hidesSearchBarWhenScrolling = searchBarHidden;
  52. }
  53. }
  54. - (void)rnn_setNavigationItemTitle:(NSString *)title {
  55. self.navigationItem.title = title;
  56. }
  57. - (void)rnn_setDrawBehindTopBar:(BOOL)drawBehind {
  58. if (drawBehind) {
  59. [self setExtendedLayoutIncludesOpaqueBars:YES];
  60. self.edgesForExtendedLayout |= UIRectEdgeTop;
  61. } else {
  62. self.edgesForExtendedLayout &= ~UIRectEdgeTop;
  63. }
  64. }
  65. - (void)rnn_setDrawBehindTabBar:(BOOL)drawBehindTabBar {
  66. if (drawBehindTabBar) {
  67. [self setExtendedLayoutIncludesOpaqueBars:YES];
  68. self.edgesForExtendedLayout |= UIRectEdgeBottom;
  69. } else {
  70. self.edgesForExtendedLayout &= ~UIRectEdgeBottom;
  71. }
  72. }
  73. - (void)rnn_setTabBarItemBadge:(NSString *)badge {
  74. UITabBarItem *tabBarItem = self.tabBarItem;
  75. if ([badge isKindOfClass:[NSNull class]] || [badge isEqualToString:@""]) {
  76. tabBarItem.badgeValue = nil;
  77. } else {
  78. tabBarItem.badgeValue = badge;
  79. [[self.tabBarController.tabBar viewWithTag:tabBarItem.tag] removeFromSuperview];
  80. tabBarItem.tag = -1;
  81. }
  82. }
  83. - (void)rnn_setTabBarItemBadgeColor:(UIColor *)badgeColor {
  84. if (@available(iOS 10.0, *)) {
  85. self.tabBarItem.badgeColor = badgeColor;
  86. }
  87. }
  88. - (void)rnn_setStatusBarStyle:(NSString *)style animated:(BOOL)animated {
  89. if (animated) {
  90. [UIView animateWithDuration:[self statusBarAnimationDuration:animated] animations:^{
  91. [self setNeedsStatusBarAppearanceUpdate];
  92. }];
  93. } else {
  94. [self setNeedsStatusBarAppearanceUpdate];
  95. }
  96. }
  97. - (void)rnn_setTopBarPrefersLargeTitle:(BOOL)prefersLargeTitle {
  98. if (@available(iOS 11.0, *)) {
  99. if (prefersLargeTitle) {
  100. self.navigationItem.largeTitleDisplayMode = UINavigationItemLargeTitleDisplayModeAlways;
  101. } else {
  102. self.navigationItem.largeTitleDisplayMode = UINavigationItemLargeTitleDisplayModeNever;
  103. }
  104. }
  105. }
  106. - (void)rnn_setStatusBarBlur:(BOOL)blur {
  107. UIView* curBlurView = [self.view viewWithTag:BLUR_STATUS_TAG];
  108. if (blur) {
  109. if (!curBlurView) {
  110. UIVisualEffectView *blur = [[UIVisualEffectView alloc] initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleLight]];
  111. blur.frame = [[UIApplication sharedApplication] statusBarFrame];
  112. blur.tag = BLUR_STATUS_TAG;
  113. [self.view insertSubview:blur atIndex:0];
  114. }
  115. } else {
  116. if (curBlurView) {
  117. [curBlurView removeFromSuperview];
  118. }
  119. }
  120. }
  121. - (void)rnn_setBackgroundColor:(UIColor *)backgroundColor {
  122. self.view.backgroundColor = backgroundColor;
  123. }
  124. - (void)rnn_setBackButtonVisible:(BOOL)visible {
  125. self.navigationItem.hidesBackButton = !visible;
  126. }
  127. - (CGFloat)statusBarAnimationDuration:(BOOL)animated {
  128. return animated ? kStatusBarAnimationDuration : CGFLOAT_MIN;
  129. }
  130. - (BOOL)isModal {
  131. if([self presentingViewController])
  132. return YES;
  133. if([[[self navigationController] presentingViewController] presentedViewController] == [self navigationController])
  134. return YES;
  135. if([[[self tabBarController] presentingViewController] isKindOfClass:[UITabBarController class]])
  136. return YES;
  137. return NO;
  138. }
  139. - (void)rnn_setInterceptTouchOutside:(BOOL)interceptTouchOutside {
  140. if ([self.view isKindOfClass:[RCTRootView class]]) {
  141. RCTRootView* rootView = (RCTRootView*)self.view;
  142. rootView.passThroughTouches = !interceptTouchOutside;
  143. }
  144. }
  145. - (void)rnn_setBackButtonIcon:(UIImage *)icon withColor:(UIColor *)color title:(NSString *)title {
  146. UIBarButtonItem *backItem = [[UIBarButtonItem alloc] init];
  147. if (icon) {
  148. backItem.image = color
  149. ? [[icon withTintColor:color] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]
  150. : icon;
  151. [self.navigationController.navigationBar setBackIndicatorImage:[UIImage new]];
  152. [self.navigationController.navigationBar setBackIndicatorTransitionMaskImage:[UIImage new]];
  153. }
  154. UIViewController *lastViewControllerInStack = self.navigationController.viewControllers.count > 1 ? self.navigationController.viewControllers[self.navigationController.viewControllers.count - 2] : self.navigationController.topViewController;
  155. backItem.title = title ? title : lastViewControllerInStack.navigationItem.title;
  156. backItem.tintColor = color;
  157. lastViewControllerInStack.navigationItem.backBarButtonItem = backItem;
  158. }
  159. - (void)applyBackButton:(RNNBackButtonOptions *)backButton {
  160. UIBarButtonItem *backItem = [UIBarButtonItem new];
  161. if (backButton.icon.hasValue) {
  162. UIColor *color = [backButton.color getWithDefaultValue:nil];
  163. backItem.image = color ?
  164. [[backButton.icon.get withTintColor:color] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] :
  165. backButton.icon.get;
  166. [self.navigationController.navigationBar setBackIndicatorImage:[UIImage new]];
  167. [self.navigationController.navigationBar setBackIndicatorTransitionMaskImage:[UIImage new]];
  168. }
  169. if ([backButton.showTitle getWithDefaultValue:YES]) backItem.title = [backButton.title getWithDefaultValue:nil];
  170. if (backButton.color.hasValue) backItem.tintColor = [backButton.color get];
  171. self.navigationItem.backBarButtonItem = backItem;
  172. }
  173. @end