react-native-navigation的迁移库

UIViewController+RNNOptions.m 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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.edgesForExtendedLayout |= UIRectEdgeTop;
  50. } else {
  51. self.edgesForExtendedLayout &= ~UIRectEdgeTop;
  52. }
  53. }
  54. - (void)rnn_setDrawBehindTabBar:(BOOL)drawBehindTabBar {
  55. if (drawBehindTabBar) {
  56. [self setExtendedLayoutIncludesOpaqueBars:YES];
  57. self.edgesForExtendedLayout |= UIRectEdgeBottom;
  58. } else {
  59. [self setExtendedLayoutIncludesOpaqueBars:NO];
  60. self.edgesForExtendedLayout &= ~UIRectEdgeBottom;
  61. }
  62. }
  63. - (void)rnn_setTabBarItemBadge:(NSString *)badge {
  64. UITabBarItem *tabBarItem = self.tabBarItem;
  65. tabBarItem.badgeValue = badge;
  66. if ([badge isKindOfClass:[NSNull class]] || [badge isEqualToString:@""]) {
  67. tabBarItem.badgeValue = nil;
  68. }
  69. }
  70. - (void)rnn_setTabBarItemBadgeColor:(UIColor *)badgeColor {
  71. self.tabBarItem.badgeColor = badgeColor;
  72. }
  73. - (void)rnn_setStatusBarStyle:(NSString *)style animated:(BOOL)animated {
  74. if (animated) {
  75. [UIView animateWithDuration:[self statusBarAnimationDuration:animated] animations:^{
  76. [self setNeedsStatusBarAppearanceUpdate];
  77. }];
  78. } else {
  79. [self setNeedsStatusBarAppearanceUpdate];
  80. }
  81. }
  82. - (void)rnn_setTopBarPrefersLargeTitle:(BOOL)prefersLargeTitle {
  83. if (@available(iOS 11.0, *)) {
  84. if (prefersLargeTitle) {
  85. self.navigationItem.largeTitleDisplayMode = UINavigationItemLargeTitleDisplayModeAlways;
  86. } else {
  87. self.navigationItem.largeTitleDisplayMode = UINavigationItemLargeTitleDisplayModeNever;
  88. }
  89. }
  90. }
  91. - (void)rnn_setStatusBarBlur:(BOOL)blur {
  92. UIView* curBlurView = [self.view viewWithTag:BLUR_STATUS_TAG];
  93. if (blur) {
  94. if (!curBlurView) {
  95. UIVisualEffectView *blur = [[UIVisualEffectView alloc] initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleLight]];
  96. blur.frame = [[UIApplication sharedApplication] statusBarFrame];
  97. blur.tag = BLUR_STATUS_TAG;
  98. [self.view insertSubview:blur atIndex:0];
  99. }
  100. } else {
  101. if (curBlurView) {
  102. [curBlurView removeFromSuperview];
  103. }
  104. }
  105. }
  106. - (void)rnn_setBackgroundColor:(UIColor *)backgroundColor {
  107. self.view.backgroundColor = backgroundColor;
  108. }
  109. - (void)rnn_setBackButtonVisible:(BOOL)visible {
  110. self.navigationItem.hidesBackButton = !visible;
  111. }
  112. - (CGFloat)statusBarAnimationDuration:(BOOL)animated {
  113. return animated ? kStatusBarAnimationDuration : CGFLOAT_MIN;
  114. }
  115. - (BOOL)isModal {
  116. if([self presentingViewController])
  117. return YES;
  118. if([[[self navigationController] presentingViewController] presentedViewController] == [self navigationController])
  119. return YES;
  120. if([[[self tabBarController] presentingViewController] isKindOfClass:[UITabBarController class]])
  121. return YES;
  122. return NO;
  123. }
  124. @end