ModalPresentationController.swift 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // MIT License
  2. //
  3. // Copyright (c) 2021 Daniel Gauthier
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in all
  13. // copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. // SOFTWARE.
  22. //
  23. // From https://github.com/danielmgauthier/ViewControllerTransitionExample
  24. // SPDX-License-Identifier: MIT
  25. import UIKit
  26. class ModalPresentationController: UIPresentationController {
  27. lazy var fadeView = {
  28. let view = UIView()
  29. view.backgroundColor = .black.withAlphaComponent(0.5)
  30. view.alpha = 0.0
  31. view.translatesAutoresizingMaskIntoConstraints = false
  32. return view
  33. }()
  34. override func presentationTransitionWillBegin() {
  35. guard let containerView = containerView else { return }
  36. containerView.insertSubview(fadeView, at: 0)
  37. NSLayoutConstraint.activate([
  38. fadeView.leadingAnchor.constraint(equalTo: containerView.leadingAnchor),
  39. fadeView.trailingAnchor.constraint(equalTo: containerView.trailingAnchor),
  40. fadeView.topAnchor.constraint(equalTo: containerView.topAnchor),
  41. fadeView.bottomAnchor.constraint(equalTo: containerView.bottomAnchor)
  42. ])
  43. guard let coordinator = presentedViewController.transitionCoordinator else {
  44. fadeView.alpha = 1.0
  45. return
  46. }
  47. coordinator.animate(alongsideTransition: { _ in
  48. self.fadeView.alpha = 1.0
  49. })
  50. }
  51. override func dismissalTransitionWillBegin() {
  52. guard let coordinator = presentedViewController.transitionCoordinator else {
  53. fadeView.alpha = 0.0
  54. return
  55. }
  56. if !coordinator.isInteractive {
  57. coordinator.animate(alongsideTransition: { _ in
  58. self.fadeView.alpha = 0.0
  59. })
  60. }
  61. }
  62. override func containerViewWillLayoutSubviews() {
  63. presentedView?.frame = frameOfPresentedViewInContainerView
  64. }
  65. override var frameOfPresentedViewInContainerView: CGRect {
  66. guard let containerView = containerView else { return .zero }
  67. var safeAreaFrame = containerView.bounds.inset(by: containerView.safeAreaInsets)
  68. // In case we have a safe are at the top, we start our frame below it, but the bottom extends beyond the bottom safe area
  69. safeAreaFrame = CGRect(x: safeAreaFrame.minX, y: safeAreaFrame.minY, width: safeAreaFrame.width, height: containerView.frame.height - safeAreaFrame.minY)
  70. return safeAreaFrame
  71. }
  72. }