react-native-navigation的迁移库

UIViewController+RNNOptions.m 4.6KB

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