react-native-navigation的迁移库

UIViewController+RNNOptions.m 5.1KB

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