react-native-navigation的迁移库

UIViewController+RNNOptions.m 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. if (@available(iOS 11.0, *)) {
  26. if (!self.navigationItem.searchController) {
  27. UISearchController *search = [[UISearchController alloc]initWithSearchResultsController:nil];
  28. search.dimsBackgroundDuringPresentation = NO;
  29. if ([self conformsToProtocol:@protocol(UISearchResultsUpdating)]) {
  30. [search setSearchResultsUpdater:((UIViewController <UISearchResultsUpdating> *) self)];
  31. }
  32. search.searchBar.delegate = (id<UISearchBarDelegate>)self;
  33. if (placeholder) {
  34. search.searchBar.placeholder = placeholder;
  35. }
  36. self.navigationItem.searchController = search;
  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)rnn_setSearchBarHiddenWhenScrolling:(BOOL)searchBarHidden {
  43. if (@available(iOS 11.0, *)) {
  44. self.navigationItem.hidesSearchBarWhenScrolling = searchBarHidden;
  45. }
  46. }
  47. - (void)rnn_setNavigationItemTitle:(NSString *)title {
  48. self.navigationItem.title = title;
  49. }
  50. - (void)rnn_setDrawBehindTopBar:(BOOL)drawBehind {
  51. if (drawBehind) {
  52. [self setExtendedLayoutIncludesOpaqueBars:YES];
  53. self.edgesForExtendedLayout |= UIRectEdgeTop;
  54. } else {
  55. self.edgesForExtendedLayout &= ~UIRectEdgeTop;
  56. }
  57. }
  58. - (void)rnn_setDrawBehindTabBar:(BOOL)drawBehindTabBar {
  59. if (drawBehindTabBar) {
  60. [self setExtendedLayoutIncludesOpaqueBars:YES];
  61. self.edgesForExtendedLayout |= UIRectEdgeBottom;
  62. } else {
  63. self.edgesForExtendedLayout &= ~UIRectEdgeBottom;
  64. }
  65. }
  66. - (void)rnn_setTabBarItemBadge:(NSString *)badge {
  67. UITabBarItem *tabBarItem = self.tabBarItem;
  68. if ([badge isKindOfClass:[NSNull class]] || [badge isEqualToString:@""]) {
  69. tabBarItem.badgeValue = nil;
  70. } else {
  71. tabBarItem.badgeValue = badge;
  72. }
  73. }
  74. - (void)rnn_setTabBarItemBadgeColor:(UIColor *)badgeColor {
  75. self.tabBarItem.badgeColor = badgeColor;
  76. }
  77. - (void)rnn_setStatusBarStyle:(NSString *)style animated:(BOOL)animated {
  78. if (animated) {
  79. [UIView animateWithDuration:[self statusBarAnimationDuration:animated] animations:^{
  80. [self setNeedsStatusBarAppearanceUpdate];
  81. }];
  82. } else {
  83. [self setNeedsStatusBarAppearanceUpdate];
  84. }
  85. }
  86. - (void)rnn_setTopBarPrefersLargeTitle:(BOOL)prefersLargeTitle {
  87. if (@available(iOS 11.0, *)) {
  88. if (prefersLargeTitle) {
  89. self.navigationItem.largeTitleDisplayMode = UINavigationItemLargeTitleDisplayModeAlways;
  90. } else {
  91. self.navigationItem.largeTitleDisplayMode = UINavigationItemLargeTitleDisplayModeNever;
  92. }
  93. }
  94. }
  95. - (void)rnn_setStatusBarBlur:(BOOL)blur {
  96. UIView* curBlurView = [self.view viewWithTag:BLUR_STATUS_TAG];
  97. if (blur) {
  98. if (!curBlurView) {
  99. UIVisualEffectView *blur = [[UIVisualEffectView alloc] initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleLight]];
  100. blur.frame = [[UIApplication sharedApplication] statusBarFrame];
  101. blur.tag = BLUR_STATUS_TAG;
  102. [self.view insertSubview:blur atIndex:0];
  103. }
  104. } else {
  105. if (curBlurView) {
  106. [curBlurView removeFromSuperview];
  107. }
  108. }
  109. }
  110. - (void)rnn_setBackgroundColor:(UIColor *)backgroundColor {
  111. self.view.backgroundColor = backgroundColor;
  112. }
  113. - (void)rnn_setBackButtonVisible:(BOOL)visible {
  114. self.navigationItem.hidesBackButton = !visible;
  115. }
  116. - (CGFloat)statusBarAnimationDuration:(BOOL)animated {
  117. return animated ? kStatusBarAnimationDuration : CGFLOAT_MIN;
  118. }
  119. - (BOOL)isModal {
  120. if([self presentingViewController])
  121. return YES;
  122. if([[[self navigationController] presentingViewController] presentedViewController] == [self navigationController])
  123. return YES;
  124. if([[[self tabBarController] presentingViewController] isKindOfClass:[UITabBarController class]])
  125. return YES;
  126. return NO;
  127. }
  128. - (void)rnn_setInterceptTouchOutside:(BOOL)interceptTouchOutside {
  129. if ([self.view isKindOfClass:[RCTRootView class]]) {
  130. RCTRootView* rootView = (RCTRootView*)self.view;
  131. rootView.passThroughTouches = !interceptTouchOutside;
  132. }
  133. }
  134. @end