react-native-navigation的迁移库

UIViewController+RNNOptions.m 6.3KB

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