ModalNavigationController.m 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. // _____ _
  2. // |_ _| |_ _ _ ___ ___ _ __ __ _
  3. // | | | ' \| '_/ -_) -_) ' \/ _` |_
  4. // |_| |_||_|_| \___\___|_|_|_\__,_(_)
  5. //
  6. // Threema iOS Client
  7. // Copyright (c) 2015-2020 Threema GmbH
  8. //
  9. // This program is free software: you can redistribute it and/or modify
  10. // it under the terms of the GNU Affero General Public License, version 3,
  11. // as published by the Free Software Foundation.
  12. //
  13. // This program is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. // GNU Affero General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU Affero General Public License
  19. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. #import "ModalNavigationController.h"
  21. @interface ModalNavigationController () <UIGestureRecognizerDelegate>
  22. @property UIBarButtonItem *doneButton;
  23. @property UITapGestureRecognizer *tapGesture;
  24. @end
  25. @implementation ModalNavigationController
  26. - (void)viewWillAppear:(BOOL)animated {
  27. [super viewWillAppear:animated];
  28. if (_showDoneButton) {
  29. [self setupDoneButton];
  30. }
  31. }
  32. - (void)viewDidAppear:(BOOL)animated
  33. {
  34. [super viewDidAppear:animated];
  35. if (_dismissOnTapOutside) {
  36. [self setupTapGesture];
  37. }
  38. }
  39. - (void)setupDoneButton {
  40. if (self.navigationBar.backItem == nil) {
  41. _doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(done:)];
  42. self.topViewController.navigationItem.leftBarButtonItem = _doneButton;
  43. }
  44. }
  45. - (void)setupTapGesture {
  46. if (_tapGesture == nil) {
  47. _tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(windowTapped:)];
  48. [_tapGesture setNumberOfTapsRequired:1];
  49. _tapGesture.cancelsTouchesInView = NO;
  50. [_tapGesture setDelegate:self];
  51. }
  52. [self.view.window addGestureRecognizer:_tapGesture];
  53. }
  54. - (void)cleanup {
  55. [self.view.window removeGestureRecognizer:_tapGesture];
  56. if (_doneButton) {
  57. _doneButton = nil;
  58. self.topViewController.navigationItem.leftBarButtonItem = nil;
  59. }
  60. }
  61. - (void)viewWillDisappear:(BOOL)animated {
  62. [self cleanup];
  63. if (_modalDelegate) {
  64. [_modalDelegate willDismissModalNavigationController];
  65. }
  66. [super viewWillDisappear:animated];
  67. }
  68. - (UIModalPresentationStyle)modalPresentationStyle {
  69. if (SYSTEM_IS_IPAD && _showFullScreenOnIPad == NO) {
  70. return UIModalPresentationFormSheet;
  71. }
  72. return [super modalPresentationStyle];
  73. }
  74. - (CGSize)preferredContentSize {
  75. return self.topViewController.preferredContentSize;
  76. }
  77. - (BOOL)shouldAutorotate {
  78. return YES;
  79. }
  80. -(UIInterfaceOrientationMask)supportedInterfaceOrientations {
  81. if (SYSTEM_IS_IPAD) {
  82. return UIInterfaceOrientationMaskAll;
  83. }
  84. return UIInterfaceOrientationMaskAllButUpsideDown;
  85. }
  86. #pragma mark - navigation button
  87. - (void)done:(id)sender
  88. {
  89. [self dismissViewControllerAnimated:YES completion:nil];
  90. }
  91. #pragma mark - UIGestureRecognizer Recognizer
  92. - (void)windowTapped:(UITapGestureRecognizer *)sender
  93. {
  94. if (sender.state == UIGestureRecognizerStateEnded)
  95. {
  96. CGPoint location = [sender locationInView:self.view];
  97. if (![self.view pointInside:location withEvent:nil]) {
  98. [self done:nil];
  99. }
  100. }
  101. }
  102. - (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
  103. return YES;
  104. }
  105. - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
  106. return YES;
  107. }
  108. - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
  109. if (self.presentedViewController.popoverPresentationController != nil) {
  110. return NO;
  111. }
  112. return YES;
  113. }
  114. @end