Created
January 23, 2018 01:10
-
-
Save alfian0/3fe77a81764f34b69cfead4a77b229ae to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
class PresentationController: UIPresentationController { | |
private var chrome: UIView = UIView() | |
override var frameOfPresentedViewInContainerView: CGRect { | |
return CGRect(x: 0, y: 0, width: containerView!.bounds.width-56, height: containerView!.bounds.height) | |
} | |
override init(presentedViewController: UIViewController, presenting presentingViewController: UIViewController?) { | |
super.init(presentedViewController: presentedViewController, presenting: presentingViewController) | |
chrome.backgroundColor = UIColor(white: 0.0, alpha: 0.5) | |
chrome.alpha = 0.0 | |
chrome.addGestureRecognizer( | |
UITapGestureRecognizer(target: self, action: #selector(tapGesture(sender:))) | |
) | |
} | |
override func containerViewWillLayoutSubviews() { | |
chrome.frame = containerView!.bounds | |
presentedView?.frame = frameOfPresentedViewInContainerView | |
} | |
override func presentationTransitionWillBegin() { | |
containerView?.insertSubview(chrome, at: 0) | |
presentedViewController | |
.transitionCoordinator? | |
.animate(alongsideTransition: { (_) in | |
self.chrome.alpha = 1.0 | |
}, completion: nil) | |
} | |
override func dismissalTransitionWillBegin() { | |
presentedViewController | |
.transitionCoordinator? | |
.animate(alongsideTransition: { (_) in | |
self.chrome.alpha = 0.0 | |
}, completion: nil) | |
} | |
@objc private func tapGesture(sender: UIGestureRecognizer) { | |
presentedViewController.dismiss(animated: true, completion: nil) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How to use