Skip to content

Commit c55f47d

Browse files
authored
Merge pull request #77 from mkko/fix/dismiss-ui-issue
Fix an issue with presentation with content visibility behavior
2 parents a43dde9 + a246cdb commit c55f47d

File tree

4 files changed

+26
-25
lines changed

4 files changed

+26
-25
lines changed

DrawerView/DrawerPresentation.swift

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ extension UIViewController: DrawerPresenter {
1919
public class DrawerPresentationController: UIPresentationController {
2020

2121
private let drawerView: DrawerView
22-
2322
private var presentationDelegate: DrawerPresentationDelegate?
23+
private var isDismissing = false
2424

2525
init(presentedViewController: UIViewController,
2626
presenting presentingViewController: UIViewController?,
@@ -76,6 +76,8 @@ public class DrawerPresentationController: UIPresentationController {
7676

7777
public override func dismissalTransitionWillBegin() {
7878
super.dismissalTransitionWillBegin()
79+
isDismissing = true
80+
7981
// Make callbacks backwards compatible
8082
if let callback = presentationDelegate?.drawerDismissalWillBegin(for:in:) {
8183
callback(presentedViewController, drawerView)
@@ -86,13 +88,8 @@ public class DrawerPresentationController: UIPresentationController {
8688

8789
public override func dismissalTransitionDidEnd(_ completed: Bool) {
8890
super.dismissalTransitionDidEnd(completed)
91+
isDismissing = false
8992

90-
// Clean up the drawer for reuse.
91-
presentedViewController.view.removeFromSuperview()
92-
presentedViewController.removeFromParent()
93-
drawerView.removeFromSuperview()
94-
95-
// Make callbacks backwards compatible
9693
if let callback = presentationDelegate?.drawerDismissalDidEnd(for:in:completed:) {
9794
callback(presentedViewController, drawerView, completed)
9895
} else {
@@ -103,10 +100,6 @@ public class DrawerPresentationController: UIPresentationController {
103100
override public var shouldRemovePresentersView: Bool {
104101
return false
105102
}
106-
107-
override public func containerViewWillLayoutSubviews() {
108-
super.containerViewWillLayoutSubviews()
109-
}
110103
}
111104

112105
@objc public protocol DrawerPresentationDelegate {
@@ -130,7 +123,8 @@ public class DrawerPresentationController: UIPresentationController {
130123
extension DrawerPresentationController: DrawerViewDelegate {
131124

132125
public func drawer(_ drawerView: DrawerView, willTransitionFrom startPosition: DrawerPosition, to targetPosition: DrawerPosition) {
133-
if targetPosition == .closed {
126+
// Only trigger dismiss if we're not already dismissing to avoid recursion
127+
if targetPosition == .closed && !isDismissing {
134128
presentedViewController.dismiss(animated: true)
135129
}
136130
}
@@ -170,6 +164,7 @@ extension DrawerPresentationManager: UIViewControllerTransitioningDelegate {
170164
public final class DrawerPresentationAnimator: NSObject {
171165

172166
let presentation: PresentationType
167+
private let animationDuration: TimeInterval = 0.3 // Standard iOS animation duration
173168

174169
enum PresentationType {
175170
case present
@@ -186,25 +181,31 @@ extension DrawerPresentationAnimator: UIViewControllerAnimatedTransitioning {
186181
public func transitionDuration(
187182
using transitionContext: UIViewControllerContextTransitioning?
188183
) -> TimeInterval {
189-
return 0.0
184+
return animationDuration
190185
}
191186

192187
public func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
193-
194188
switch presentation {
195189
case .present:
196190
guard let drawerView = transitionContext.view(forKey: .to) as? DrawerView else {
191+
transitionContext.completeTransition(false)
197192
return
198193
}
199194

200195
drawerView.setPosition(.open, animated: true) { finished in
201-
transitionContext.completeTransition(finished)
202-
}
196+
transitionContext.completeTransition(finished)
197+
}
203198
case .dismiss:
204199
guard let drawerView = transitionContext.view(forKey: .from) as? DrawerView else {
200+
transitionContext.completeTransition(false)
205201
return
206202
}
203+
204+
let originalVisibilityBehavior = drawerView.contentVisibilityBehavior
205+
drawerView.contentVisibilityBehavior = .never
206+
207207
drawerView.setPosition(.closed, animated: true) { finished in
208+
drawerView.contentVisibilityBehavior = originalVisibilityBehavior
208209
transitionContext.completeTransition(finished)
209210
}
210211
}

DrawerView/DrawerView.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -498,7 +498,7 @@ private struct ChildScrollViewInfo {
498498
}
499499

500500
func embed(view: UIView) {
501-
view.backgroundColor = .clear
501+
// view.backgroundColor = .clear
502502
view.frame = self.bounds
503503
view.translatesAutoresizingMaskIntoConstraints = false
504504
self.addSubview(view)

Example/DrawerViewExample/Base.lproj/Main.storyboard

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="22154" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="BYZ-38-t0r">
2+
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="23504" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="BYZ-38-t0r">
33
<device id="retina4_7" orientation="portrait" appearance="light"/>
44
<dependencies>
5-
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="22130"/>
5+
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="23506"/>
66
<capability name="Safe area layout guides" minToolsVersion="9.0"/>
77
<capability name="Stack View standard spacing" minToolsVersion="9.0"/>
88
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
@@ -211,7 +211,7 @@
211211
<constraint firstAttribute="trailing" secondItem="com-Ya-LsP" secondAttribute="trailing" constant="20" id="HMD-C4-HQX"/>
212212
<constraint firstAttribute="trailing" secondItem="uQc-Da-g9m" secondAttribute="trailing" constant="16" id="I5o-QI-hRy"/>
213213
<constraint firstItem="uQc-Da-g9m" firstAttribute="leading" secondItem="A0d-xS-Dle" secondAttribute="leading" constant="16" id="f3F-Xr-zYx"/>
214-
<constraint firstItem="uQc-Da-g9m" firstAttribute="top" secondItem="QP1-4T-vBO" secondAttribute="topMargin" constant="80" id="ncf-2F-Vyz"/>
214+
<constraint firstItem="uQc-Da-g9m" firstAttribute="top" secondItem="QP1-4T-vBO" secondAttribute="top" constant="100" id="ncf-2F-Vyz"/>
215215
<constraint firstItem="com-Ya-LsP" firstAttribute="leading" relation="greaterThanOrEqual" secondItem="A0d-xS-Dle" secondAttribute="leading" constant="20" id="vwH-J4-76C"/>
216216
<constraint firstItem="Wev-iY-Wa9" firstAttribute="centerY" secondItem="com-Ya-LsP" secondAttribute="centerY" id="wQs-EA-Fhw"/>
217217
</constraints>
@@ -222,7 +222,7 @@
222222
</viewController>
223223
<placeholder placeholderIdentifier="IBFirstResponder" id="AU3-g5-75i" userLabel="First Responder" sceneMemberID="firstResponder"/>
224224
</objects>
225-
<point key="canvasLocation" x="1976.8" y="133.5832083958021"/>
225+
<point key="canvasLocation" x="1976.8" y="133.13343328335833"/>
226226
</scene>
227227
</scenes>
228228
</document>

Example/DrawerViewExample/ViewController.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,12 +112,12 @@ class ViewController: UIViewController {
112112
}
113113
}
114114

115-
let drawerPresentation = DrawerPresentationManager()
115+
let settingsPresentation = DrawerPresentationManager()
116116

117117
private func presentDrawer() {
118-
let viewController = self.storyboard!.instantiateViewController(withIdentifier: "ModalPresentationViewController") as! ModalPresentationViewController
119-
drawerPresentation.drawer.openHeightBehavior = .fitting
120-
viewController.transitioningDelegate = drawerPresentation
118+
let viewController = UIViewController()
119+
// settingsPresentation.drawer.openHeightBehavior = .fitting
120+
viewController.transitioningDelegate = settingsPresentation
121121
viewController.modalPresentationStyle = .custom
122122
self.present(viewController, animated: true, completion: nil)
123123
}

0 commit comments

Comments
 (0)