FLEXNavigationController.m 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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 dismiss if not presented with a sheet style
  31. if (@available(iOS 13, *)) {
  32. switch (self.modalPresentationStyle) {
  33. case UIModalPresentationAutomatic:
  34. case UIModalPresentationPageSheet:
  35. case UIModalPresentationFormSheet:
  36. break;
  37. default:
  38. [self addNavigationBarSwipeGesture];
  39. break;
  40. }
  41. } else {
  42. [self addNavigationBarSwipeGesture];
  43. }
  44. }
  45. - (void)viewWillAppear:(BOOL)animated {
  46. [super viewWillAppear:animated];
  47. if (self.beingPresented && !self.didSetupPendingDismissButtons) {
  48. for (UIViewController *vc in self.viewControllers) {
  49. [self addNavigationBarItemsToViewController:vc.navigationItem];
  50. }
  51. self.didSetupPendingDismissButtons = YES;
  52. }
  53. }
  54. - (void)viewDidAppear:(BOOL)animated {
  55. [super viewDidAppear:animated];
  56. if (self.waitingToAddTab) {
  57. // Only add new tab if we're presented properly
  58. if ([self.presentingViewController isKindOfClass:[FLEXExplorerViewController class]]) {
  59. // New navigation controllers always add themselves as new tabs,
  60. // tabs are closed by FLEXExplorerViewController
  61. [FLEXTabList.sharedList addTab:self];
  62. self.waitingToAddTab = NO;
  63. }
  64. }
  65. }
  66. - (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated {
  67. [super pushViewController:viewController animated:animated];
  68. [self addNavigationBarItemsToViewController:viewController.navigationItem];
  69. }
  70. - (void)dismissAnimated {
  71. // Tabs are only closed if the done button is pressed; this
  72. // allows you to leave a tab open by dragging down to dismiss
  73. [FLEXTabList.sharedList closeTab:self];
  74. [self.presentingViewController dismissViewControllerAnimated:YES completion:nil];
  75. }
  76. - (void)addNavigationBarItemsToViewController:(UINavigationItem *)navigationItem {
  77. if (!self.presentingViewController) {
  78. return;
  79. }
  80. // Check if a done item already exists
  81. for (UIBarButtonItem *item in navigationItem.rightBarButtonItems) {
  82. if (item.style == UIBarButtonItemStyleDone) {
  83. return;
  84. }
  85. }
  86. // Give root view controllers a Done button if it does not already have one
  87. UIBarButtonItem *done = [[UIBarButtonItem alloc]
  88. initWithBarButtonSystemItem:UIBarButtonSystemItemDone
  89. target:self
  90. action:@selector(dismissAnimated)
  91. ];
  92. // Prepend the button if other buttons exist already
  93. NSArray *existingItems = navigationItem.rightBarButtonItems;
  94. if (existingItems.count) {
  95. navigationItem.rightBarButtonItems = [@[done] arrayByAddingObjectsFromArray:existingItems];
  96. } else {
  97. navigationItem.rightBarButtonItem = done;
  98. }
  99. // Keeps us from calling this method again on
  100. // the same view controllers in -viewWillAppear:
  101. self.didSetupPendingDismissButtons = YES;
  102. }
  103. - (void)addNavigationBarSwipeGesture {
  104. UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc]
  105. initWithTarget:self action:@selector(handleNavigationBarSwipe:)
  106. ];
  107. swipe.direction = UISwipeGestureRecognizerDirectionDown;
  108. swipe.delegate = self;
  109. self.navigationBarSwipeGesture = swipe;
  110. [self.navigationBar addGestureRecognizer:swipe];
  111. }
  112. - (void)handleNavigationBarSwipe:(UISwipeGestureRecognizer *)sender {
  113. [self.presentingViewController dismissViewControllerAnimated:YES completion:nil];
  114. }
  115. - (BOOL)gestureRecognizer:(UIGestureRecognizer *)g1 shouldBeRequiredToFailByGestureRecognizer:(UIGestureRecognizer *)g2 {
  116. if (g1 == self.navigationBarSwipeGesture && g2 == self.barHideOnSwipeGestureRecognizer) {
  117. return YES;
  118. }
  119. return NO;
  120. }
  121. - (void)_gestureRecognizedInteractiveHide:(UIPanGestureRecognizer *)sender {
  122. if (sender.state == UIGestureRecognizerStateRecognized) {
  123. BOOL show = self.topViewController.toolbarItems.count;
  124. CGFloat yTranslation = [sender translationInView:self.view].y;
  125. CGFloat yVelocity = [sender velocityInView:self.view].y;
  126. if (yVelocity > 2000) {
  127. [self setToolbarHidden:YES animated:YES];
  128. } else if (show && yTranslation > 20 && yVelocity > 250) {
  129. [self setToolbarHidden:NO animated:YES];
  130. } else if (yTranslation < -20) {
  131. [self setToolbarHidden:YES animated:YES];
  132. }
  133. }
  134. }
  135. @end