react-native-navigation的迁移库

UIViewController+RNNOptions.m 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. #import "UIViewController+RNNOptions.h"
  2. #import <React/RCTRootView.h>
  3. #import "UIImage+tint.h"
  4. #define kStatusBarAnimationDuration 0.35
  5. const NSInteger BLUR_STATUS_TAG = 78264801;
  6. @implementation UIViewController (RNNOptions)
  7. - (void)rnn_setBackgroundImage:(UIImage *)backgroundImage {
  8. if (backgroundImage) {
  9. UIImageView* backgroundImageView = (self.view.subviews.count > 0) ? self.view.subviews[0] : nil;
  10. if (![backgroundImageView isKindOfClass:[UIImageView class]]) {
  11. backgroundImageView = [[UIImageView alloc] initWithFrame:self.view.bounds];
  12. [self.view insertSubview:backgroundImageView atIndex:0];
  13. }
  14. backgroundImageView.layer.masksToBounds = YES;
  15. backgroundImageView.image = backgroundImage;
  16. [backgroundImageView setContentMode:UIViewContentModeScaleAspectFill];
  17. }
  18. }
  19. - (void)rnn_setModalPresentationStyle:(UIModalPresentationStyle)modalPresentationStyle {
  20. self.modalPresentationStyle = modalPresentationStyle;
  21. }
  22. - (void)rnn_setModalTransitionStyle:(UIModalTransitionStyle)modalTransitionStyle {
  23. self.modalTransitionStyle = modalTransitionStyle;
  24. }
  25. - (void)rnn_setSearchBarWithPlaceholder:(NSString *)placeholder
  26. hideNavBarOnFocusSearchBar:(BOOL)hideNavBarOnFocusSearchBar {
  27. if (@available(iOS 11.0, *)) {
  28. if (!self.navigationItem.searchController) {
  29. UISearchController *search = [[UISearchController alloc]initWithSearchResultsController:nil];
  30. search.dimsBackgroundDuringPresentation = NO;
  31. if ([self conformsToProtocol:@protocol(UISearchResultsUpdating)]) {
  32. [search setSearchResultsUpdater:((UIViewController <UISearchResultsUpdating> *) self)];
  33. }
  34. search.searchBar.delegate = (id<UISearchBarDelegate>)self;
  35. if (placeholder) {
  36. search.searchBar.placeholder = placeholder;
  37. }
  38. search.hidesNavigationBarDuringPresentation = hideNavBarOnFocusSearchBar;
  39. self.navigationItem.searchController = search;
  40. [self.navigationItem setHidesSearchBarWhenScrolling:NO];
  41. // Fixes #3450, otherwise, UIKit will infer the presentation context to be the root most view controller
  42. self.definesPresentationContext = YES;
  43. }
  44. }
  45. }
  46. - (void)rnn_setSearchBarHiddenWhenScrolling:(BOOL)searchBarHidden {
  47. if (@available(iOS 11.0, *)) {
  48. self.navigationItem.hidesSearchBarWhenScrolling = searchBarHidden;
  49. }
  50. }
  51. - (void)rnn_setNavigationItemTitle:(NSString *)title {
  52. self.navigationItem.title = title;
  53. }
  54. - (void)rnn_setDrawBehindTopBar:(BOOL)drawBehind {
  55. if (drawBehind) {
  56. [self setExtendedLayoutIncludesOpaqueBars:YES];
  57. self.edgesForExtendedLayout |= UIRectEdgeTop;
  58. } else {
  59. self.edgesForExtendedLayout &= ~UIRectEdgeTop;
  60. }
  61. }
  62. - (void)rnn_setDrawBehindTabBar:(BOOL)drawBehindTabBar {
  63. if (drawBehindTabBar) {
  64. [self setExtendedLayoutIncludesOpaqueBars:YES];
  65. self.edgesForExtendedLayout |= UIRectEdgeBottom;
  66. } else {
  67. self.edgesForExtendedLayout &= ~UIRectEdgeBottom;
  68. }
  69. }
  70. - (void)rnn_setTabBarItemBadge:(NSString *)badge {
  71. UITabBarItem *tabBarItem = self.tabBarItem;
  72. if ([badge isKindOfClass:[NSNull class]] || [badge isEqualToString:@""]) {
  73. tabBarItem.badgeValue = nil;
  74. } else {
  75. tabBarItem.badgeValue = badge;
  76. }
  77. }
  78. - (void)rnn_setTabBarItemBadgeColor:(UIColor *)badgeColor {
  79. if (@available(iOS 10.0, *)) {
  80. self.tabBarItem.badgeColor = badgeColor;
  81. }
  82. }
  83. - (void)rnn_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)rnn_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)rnn_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)rnn_setBackgroundColor:(UIColor *)backgroundColor {
  117. self.view.backgroundColor = backgroundColor;
  118. }
  119. - (void)rnn_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)rnn_setInterceptTouchOutside:(BOOL)interceptTouchOutside {
  135. if ([self.view isKindOfClass:[RCTRootView class]]) {
  136. RCTRootView* rootView = (RCTRootView*)self.view;
  137. rootView.passThroughTouches = !interceptTouchOutside;
  138. }
  139. }
  140. - (void)rnn_setBackButtonIcon:(UIImage *)icon withColor:(UIColor *)color title:(NSString *)title {
  141. UIBarButtonItem *backItem = [[UIBarButtonItem alloc] init];
  142. if (icon) {
  143. backItem.image = color
  144. ? [[icon withTintColor:color] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]
  145. : icon;
  146. [self.navigationController.navigationBar setBackIndicatorImage:[UIImage new]];
  147. [self.navigationController.navigationBar setBackIndicatorTransitionMaskImage:[UIImage new]];
  148. }
  149. UIViewController *lastViewControllerInStack = self.navigationController.viewControllers.count > 1 ? [self.navigationController.viewControllers objectAtIndex:self.navigationController.viewControllers.count-2] : self.navigationController.topViewController;
  150. backItem.title = title ? title : lastViewControllerInStack.navigationItem.title;
  151. backItem.tintColor = color;
  152. lastViewControllerInStack.navigationItem.backBarButtonItem = backItem;
  153. }
  154. @end