FLEXNavigationController.m 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. //
  2. // FLEXNavigationController.m
  3. // FLEX
  4. //
  5. // Created by Tanner on 1/30/20.
  6. // Copyright © 2020 Flipboard. All rights reserved.
  7. //
  8. #import "FLEXNavigationController.h"
  9. #import "FLEXExplorerViewController.h"
  10. #import "FLEXTabList.h"
  11. @interface UINavigationController (Private) <UIGestureRecognizerDelegate>
  12. - (void)_gestureRecognizedInteractiveHide:(UIGestureRecognizer *)sender;
  13. @end
  14. @interface UIPanGestureRecognizer (Private)
  15. - (void)_setDelegate:(id)delegate;
  16. @end
  17. @interface FLEXNavigationController ()
  18. @property (nonatomic, readonly) BOOL toolbarWasHidden;
  19. @property (nonatomic) BOOL waitingToAddTab;
  20. @property (nonatomic) BOOL didSetupPendingDismissButtons;
  21. @property (nonatomic) UISwipeGestureRecognizer *navigationBarSwipeGesture;
  22. @end
  23. @implementation FLEXNavigationController
  24. + (instancetype)withRootViewController:(UIViewController *)rootVC {
  25. return [[self alloc] initWithRootViewController:rootVC];
  26. }
  27. - (void)viewDidLoad {
  28. [super viewDidLoad];
  29. self.waitingToAddTab = YES;
  30. // Add gesture to reveal toolbar if hidden
  31. self.navigationBar.userInteractionEnabled = YES;
  32. [self.navigationBar addGestureRecognizer:[[UITapGestureRecognizer alloc]
  33. initWithTarget:self action:@selector(handleNavigationBarTap:)
  34. ]];
  35. // Add gesture to dismiss if not presented with a sheet style
  36. if (@available(iOS 13, *)) {
  37. switch (self.modalPresentationStyle) {
  38. case UIModalPresentationAutomatic:
  39. case UIModalPresentationPageSheet:
  40. case UIModalPresentationFormSheet:
  41. break;
  42. default:
  43. [self addNavigationBarSwipeGesture];
  44. break;
  45. }
  46. } else {
  47. [self addNavigationBarSwipeGesture];
  48. }
  49. }
  50. - (void)viewWillAppear:(BOOL)animated {
  51. [super viewWillAppear:animated];
  52. if (self.beingPresented && !self.didSetupPendingDismissButtons) {
  53. for (UIViewController *vc in self.viewControllers) {
  54. [self addNavigationBarItemsToViewController:vc.navigationItem];
  55. }
  56. self.didSetupPendingDismissButtons = YES;
  57. }
  58. }
  59. - (void)viewDidAppear:(BOOL)animated {
  60. [super viewDidAppear:animated];
  61. if (self.waitingToAddTab) {
  62. // Only add new tab if we're presented properly
  63. if ([self.presentingViewController isKindOfClass:[FLEXExplorerViewController class]]) {
  64. // New navigation controllers always add themselves as new tabs,
  65. // tabs are closed by FLEXExplorerViewController
  66. [FLEXTabList.sharedList addTab:self];
  67. self.waitingToAddTab = NO;
  68. }
  69. }
  70. }
  71. - (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated {
  72. [super pushViewController:viewController animated:animated];
  73. [self addNavigationBarItemsToViewController:viewController.navigationItem];
  74. }
  75. - (void)dismissAnimated {
  76. // Tabs are only closed if the done button is pressed; this
  77. // allows you to leave a tab open by dragging down to dismiss
  78. [FLEXTabList.sharedList closeTab:self];
  79. [self.presentingViewController dismissViewControllerAnimated:YES completion:nil];
  80. }
  81. - (void)addNavigationBarItemsToViewController:(UINavigationItem *)navigationItem {
  82. if (!self.presentingViewController) {
  83. return;
  84. }
  85. // Check if a done item already exists
  86. for (UIBarButtonItem *item in navigationItem.rightBarButtonItems) {
  87. if (item.style == UIBarButtonItemStyleDone) {
  88. return;
  89. }
  90. }
  91. // Give root view controllers a Done button if it does not already have one
  92. UIBarButtonItem *done = [[UIBarButtonItem alloc]
  93. initWithBarButtonSystemItem:UIBarButtonSystemItemDone
  94. target:self
  95. action:@selector(dismissAnimated)
  96. ];
  97. // Prepend the button if other buttons exist already
  98. NSArray *existingItems = navigationItem.rightBarButtonItems;
  99. if (existingItems.count) {
  100. navigationItem.rightBarButtonItems = [@[done] arrayByAddingObjectsFromArray:existingItems];
  101. } else {
  102. navigationItem.rightBarButtonItem = done;
  103. }
  104. // Keeps us from calling this method again on
  105. // the same view controllers in -viewWillAppear:
  106. self.didSetupPendingDismissButtons = YES;
  107. }
  108. - (void)addNavigationBarSwipeGesture {
  109. UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc]
  110. initWithTarget:self action:@selector(handleNavigationBarSwipe:)
  111. ];
  112. swipe.direction = UISwipeGestureRecognizerDirectionDown;
  113. swipe.delegate = self;
  114. self.navigationBarSwipeGesture = swipe;
  115. [self.navigationBar addGestureRecognizer:swipe];
  116. }
  117. - (void)handleNavigationBarSwipe:(UISwipeGestureRecognizer *)sender {
  118. if (sender.state == UIGestureRecognizerStateRecognized) {
  119. [self.presentingViewController dismissViewControllerAnimated:YES completion:nil];
  120. }
  121. }
  122. - (void)handleNavigationBarTap:(UIGestureRecognizer *)sender {
  123. if (sender.state == UIGestureRecognizerStateRecognized) {
  124. if (self.toolbarHidden) {
  125. [self setToolbarHidden:NO animated:YES];
  126. }
  127. }
  128. }
  129. - (BOOL)gestureRecognizer:(UIGestureRecognizer *)g1 shouldBeRequiredToFailByGestureRecognizer:(UIGestureRecognizer *)g2 {
  130. if (g1 == self.navigationBarSwipeGesture && g2 == self.barHideOnSwipeGestureRecognizer) {
  131. return YES;
  132. }
  133. return NO;
  134. }
  135. - (void)_gestureRecognizedInteractiveHide:(UIPanGestureRecognizer *)sender {
  136. if (sender.state == UIGestureRecognizerStateRecognized) {
  137. BOOL show = self.topViewController.toolbarItems.count;
  138. CGFloat yTranslation = [sender translationInView:self.view].y;
  139. CGFloat yVelocity = [sender velocityInView:self.view].y;
  140. if (yVelocity > 2000) {
  141. [self setToolbarHidden:YES animated:YES];
  142. } else if (show && yTranslation > 20 && yVelocity > 250) {
  143. [self setToolbarHidden:NO animated:YES];
  144. } else if (yTranslation < -20) {
  145. [self setToolbarHidden:YES animated:YES];
  146. }
  147. }
  148. }
  149. @end