react-native-navigation的迁移库

UIViewController+RNNOptions.m 5.1KB

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