Skip to content

Commit 4d3fe42

Browse files
committed
DEMO APP: Add switch to show middle position
1 parent 548e82e commit 4d3fe42

File tree

3 files changed

+55
-11
lines changed

3 files changed

+55
-11
lines changed

Example/UIViewController-DisplayInDrawer/Base.lproj/Main.storyboard

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
4242
<subviews>
4343
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="gE4-qu-Fbg">
44-
<rect key="frame" x="62.5" y="8" width="35" height="44"/>
44+
<rect key="frame" x="16" y="8" width="35" height="44"/>
4545
<constraints>
4646
<constraint firstAttribute="height" constant="44" id="LSn-eT-hFL"/>
4747
</constraints>
@@ -51,7 +51,7 @@
5151
</connections>
5252
</button>
5353
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="n2S-kO-F6i">
54-
<rect key="frame" x="20" y="60" width="120" height="44"/>
54+
<rect key="frame" x="59" y="8" width="120" height="44"/>
5555
<constraints>
5656
<constraint firstAttribute="height" constant="44" id="Jk5-Ug-mff"/>
5757
</constraints>
@@ -72,23 +72,43 @@
7272
<constraint firstAttribute="width" secondItem="WHI-2I-FcN" secondAttribute="height" multiplier="1588:1008" id="cF5-Q9-QYV"/>
7373
</constraints>
7474
</imageView>
75+
<switch opaque="NO" contentMode="scaleToFill" horizontalHuggingPriority="750" verticalHuggingPriority="750" contentHorizontalAlignment="center" contentVerticalAlignment="center" translatesAutoresizingMaskIntoConstraints="NO" id="cye-0H-NTU">
76+
<rect key="frame" x="130" y="59" width="51" height="31"/>
77+
<connections>
78+
<action selector="useMiddlePositionSwitchDidChangeValue:" destination="vXZ-lx-hvc" eventType="valueChanged" id="szN-08-Ppg"/>
79+
</connections>
80+
</switch>
81+
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="use middle position" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="hsr-oB-V4F">
82+
<rect key="frame" x="16" y="67" width="110" height="14.5"/>
83+
<fontDescription key="fontDescription" type="system" pointSize="12"/>
84+
<nil key="textColor"/>
85+
<nil key="highlightedColor"/>
86+
</label>
7587
</subviews>
7688
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
7789
<constraints>
78-
<constraint firstItem="n2S-kO-F6i" firstAttribute="centerX" secondItem="kh9-bI-dsS" secondAttribute="centerX" multiplier="0.5" id="1Wd-Ls-ASv"/>
7990
<constraint firstItem="9aL-bC-GCL" firstAttribute="width" secondItem="kh9-bI-dsS" secondAttribute="width" multiplier="0.38" priority="999" id="3Ij-WH-oeg"/>
80-
<constraint firstItem="n2S-kO-F6i" firstAttribute="top" secondItem="gE4-qu-Fbg" secondAttribute="bottom" constant="8" id="46E-Pa-N96"/>
91+
<constraint firstItem="hsr-oB-V4F" firstAttribute="leading" secondItem="kh9-bI-dsS" secondAttribute="leadingMargin" id="4nW-J5-0mh"/>
92+
<constraint firstItem="cye-0H-NTU" firstAttribute="centerY" secondItem="hsr-oB-V4F" secondAttribute="centerY" id="9q3-nv-XpF"/>
93+
<constraint firstItem="cye-0H-NTU" firstAttribute="trailing" secondItem="n2S-kO-F6i" secondAttribute="trailing" id="B06-F6-Yjn"/>
8194
<constraint firstItem="WHI-2I-FcN" firstAttribute="width" secondItem="kh9-bI-dsS" secondAttribute="width" id="BQz-L6-pQX"/>
95+
<constraint firstItem="gE4-qu-Fbg" firstAttribute="leading" secondItem="kh9-bI-dsS" secondAttribute="leadingMargin" id="C2y-IK-33N"/>
8296
<constraint firstAttribute="trailingMargin" secondItem="9aL-bC-GCL" secondAttribute="trailing" id="C6x-nZ-IJW"/>
97+
<constraint firstItem="hsr-oB-V4F" firstAttribute="top" secondItem="gE4-qu-Fbg" secondAttribute="bottom" constant="15" id="FEW-r5-nD2"/>
8398
<constraint firstItem="WHI-2I-FcN" firstAttribute="top" secondItem="9aL-bC-GCL" secondAttribute="bottom" constant="8" id="KoQ-kH-Qqy"/>
8499
<constraint firstItem="WHI-2I-FcN" firstAttribute="centerX" secondItem="kh9-bI-dsS" secondAttribute="centerX" id="L9z-XR-MVL"/>
85-
<constraint firstItem="gE4-qu-Fbg" firstAttribute="centerX" secondItem="kh9-bI-dsS" secondAttribute="centerX" multiplier="0.5" id="gpP-x9-sOL"/>
100+
<constraint firstItem="cye-0H-NTU" firstAttribute="leading" relation="greaterThanOrEqual" secondItem="hsr-oB-V4F" secondAttribute="trailing" constant="4" id="dDG-3A-47S"/>
101+
<constraint firstItem="n2S-kO-F6i" firstAttribute="centerY" secondItem="gE4-qu-Fbg" secondAttribute="centerY" id="g3d-Et-AI6"/>
102+
<constraint firstItem="n2S-kO-F6i" firstAttribute="leading" secondItem="gE4-qu-Fbg" secondAttribute="trailing" constant="8" id="jOe-kH-kiv"/>
86103
<constraint firstItem="gE4-qu-Fbg" firstAttribute="top" secondItem="jyV-Pf-zRb" secondAttribute="bottom" constant="8" id="nN1-xu-vOF"/>
87104
<constraint firstAttribute="bottom" secondItem="WHI-2I-FcN" secondAttribute="bottom" id="qxc-6B-GgV"/>
88105
</constraints>
89106
</view>
90107
<extendedEdge key="edgesForExtendedLayout" bottom="YES"/>
91108
<navigationItem key="navigationItem" id="xRd-lz-Ipx"/>
109+
<connections>
110+
<outlet property="useMiddlePositionSwitch" destination="cye-0H-NTU" id="7ww-jB-jah"/>
111+
</connections>
92112
</viewController>
93113
<placeholder placeholderIdentifier="IBFirstResponder" id="x5A-6p-PRh" sceneMemberID="firstResponder"/>
94114
</objects>

Example/UIViewController-DisplayInDrawer/ContentViewController.swift

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import UIViewController_DisplayInDrawer
55

66
enum ContentMode {
77
case fullScreen
8-
case drawer
8+
case drawer(useMiddlePosition: Bool)
99

1010
var config: ContentConfig {
1111
switch self {
@@ -57,7 +57,7 @@ class ContentViewController: UIViewController {
5757
setupMode()
5858
}
5959

60-
@IBAction private func dismiss() {
60+
@IBAction func dismiss() {
6161
drawerDismissClosure?()
6262
}
6363

@@ -81,13 +81,17 @@ extension ContentViewController: DrawerConfiguration {
8181
}
8282

8383
func middlePositionY(for parentHeight: CGFloat) -> CGFloat? {
84-
guard isViewLoaded else { return nil }
85-
return parentHeight - separatorView.frame.minY
84+
guard isViewLoaded, useMiddlePosition else { return nil }
85+
return halfOpenPositionY(for: parentHeight)
8686
}
8787

8888
func bottomPositionY(for parentHeight: CGFloat) -> CGFloat {
8989
guard isViewLoaded else { return 0 }
90-
return parentHeight - 18
90+
if useMiddlePosition {
91+
return parentHeight - 18
92+
} else {
93+
return halfOpenPositionY(for: parentHeight)
94+
}
9195
}
9296

9397
func setPanGestureTarget(_ target: Any, action: Selector) {
@@ -101,6 +105,19 @@ extension ContentViewController: DrawerConfiguration {
101105
let bottomPadding: CGFloat = 16
102106
return fixedContentHeight + scrollableContentHeight + bottomPadding
103107
}
108+
109+
private var useMiddlePosition: Bool {
110+
if let mode = mode,
111+
case let ContentMode.drawer(useMiddlePosition) = mode {
112+
return useMiddlePosition
113+
} else {
114+
return false
115+
}
116+
}
117+
118+
private func halfOpenPositionY(for parentHeight: CGFloat) -> CGFloat {
119+
return parentHeight - separatorView.frame.minY
120+
}
104121
}
105122

106123
extension UITextView {

Example/UIViewController-DisplayInDrawer/MainViewController.swift

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import UIKit
44
import UIViewController_DisplayInDrawer
55

66
class MainViewController: UIViewController {
7+
@IBOutlet weak var useMiddlePositionSwitch: UISwitch!
8+
weak var drawerContentController: ContentViewController?
79
@IBAction func push(_ sender: Any) {
810
let controller = makeContentViewController()
911
controller.setup(for: .fullScreen)
@@ -12,15 +14,20 @@ class MainViewController: UIViewController {
1214

1315
@IBAction func presentInDrawer(_ sender: Any) {
1416
let controller = makeContentViewController()
15-
controller.setup(for: .drawer)
17+
controller.setup(for: .drawer(useMiddlePosition: useMiddlePositionSwitch.isOn))
1618
navigationController?.displayInDrawer(controller, drawerPositionDelegate: self)
19+
drawerContentController = controller
1720
}
1821

1922
private func makeContentViewController() -> ContentViewController {
2023
let storyboard = UIStoryboard(name: "Content", bundle: nil)
2124
let result = storyboard.instantiateInitialViewController() as! ContentViewController
2225
return result
2326
}
27+
28+
@IBAction func useMiddlePositionSwitchDidChangeValue(_ sender: Any) {
29+
drawerContentController?.dismiss()
30+
}
2431
}
2532

2633
extension MainViewController: DrawerPositionDelegate {

0 commit comments

Comments
 (0)