Skip to content
This repository was archived by the owner on Aug 13, 2021. It is now read-only.

Commit 40decaf

Browse files
author
Jeff Verkoeyen
committed
Update modal dialog example to make use of a presentation controller.
Reviewers: O2 Material Motion, O4 Material Apple platform reviewers, #material_motion, randcode-generator Reviewed By: randcode-generator Tags: #material_motion Differential Revision: http://codereview.cc/D3242
1 parent e0090c9 commit 40decaf

File tree

1 file changed

+28
-3
lines changed

1 file changed

+28
-3
lines changed

examples/ModalDialogExample.swift

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,6 @@ class ModalDialogViewController: UIViewController {
4646
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
4747

4848
transitionController.transition = ModalDialogTransition()
49-
preferredContentSize = .init(width: 200, height: 200)
50-
modalPresentationStyle = .overCurrentContext
5149
}
5250

5351
required init?(coder aDecoder: NSCoder) {
@@ -67,7 +65,19 @@ class ModalDialogViewController: UIViewController {
6765
}
6866
}
6967

70-
class ModalDialogTransition: SelfDismissingTransition {
68+
class ModalDialogTransition: SelfDismissingTransition, TransitionWithPresentation {
69+
70+
public func defaultModalPresentationStyle() -> UIModalPresentationStyle? {
71+
return .custom
72+
}
73+
74+
public func presentationController(forPresented presented: UIViewController,
75+
presenting: UIViewController?,
76+
source: UIViewController) -> UIPresentationController? {
77+
return ModalDialogPresentationController(presentedViewController: presented,
78+
presenting: presenting)
79+
}
80+
7181

7282
func willBeginTransition(withContext ctx: TransitionContext, runtime: MotionRuntime) -> [Stateful] {
7383
let size = ctx.fore.view.frame.size
@@ -113,3 +123,18 @@ class ModalDialogTransition: SelfDismissingTransition {
113123
dismisser.dismissWhenGestureRecognizerBegins(pan)
114124
}
115125
}
126+
127+
private final class ModalDialogPresentationController: UIPresentationController {
128+
129+
override var frameOfPresentedViewInContainerView: CGRect {
130+
guard let containerView = containerView else {
131+
assertionFailure("Missing container view during frame query.")
132+
return .zero()
133+
}
134+
let size = CGSize(width: 200, height: 200)
135+
return CGRect(x: containerView.bounds.midX - size.width / 2,
136+
y: containerView.bounds.midY - size.height / 2,
137+
width: size.width,
138+
height: size.height)
139+
}
140+
}

0 commit comments

Comments
 (0)