Skip to content

Commit 11d0871

Browse files
author
Vilém Kurz
authored
Merge pull request #7 from inloop/feature/update-demo-app-with-new-functions
Feature/update demo app with new functions
2 parents 548e82e + 8ffd45e commit 11d0871

File tree

4 files changed

+130
-13
lines changed

4 files changed

+130
-13
lines changed

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

Lines changed: 43 additions & 7 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>
@@ -61,7 +61,7 @@
6161
</connections>
6262
</button>
6363
<imageView opaque="NO" userInteractionEnabled="NO" contentMode="scaleToFill" horizontalHuggingPriority="251" verticalHuggingPriority="251" image="sun" translatesAutoresizingMaskIntoConstraints="NO" id="9aL-bC-GCL">
64-
<rect key="frame" x="182" y="83" width="121.5" height="121.5"/>
64+
<rect key="frame" x="182.5" y="83" width="121.5" height="121.5"/>
6565
<constraints>
6666
<constraint firstAttribute="width" secondItem="9aL-bC-GCL" secondAttribute="height" multiplier="1:1" id="9fQ-wl-ggV"/>
6767
</constraints>
@@ -72,27 +72,63 @@
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>
87+
<button opaque="NO" contentMode="scaleToFill" enabled="NO" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="L4w-pG-YSK">
88+
<rect key="frame" x="187" y="8" width="110" height="44"/>
89+
<constraints>
90+
<constraint firstAttribute="height" constant="44" id="rbb-OR-JVg"/>
91+
</constraints>
92+
<state key="normal" title="Change content"/>
93+
<connections>
94+
<action selector="changeContentButtonPressed:" destination="vXZ-lx-hvc" eventType="touchUpInside" id="VYf-DU-oi2"/>
95+
</connections>
96+
</button>
7597
</subviews>
7698
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
7799
<constraints>
78-
<constraint firstItem="n2S-kO-F6i" firstAttribute="centerX" secondItem="kh9-bI-dsS" secondAttribute="centerX" multiplier="0.5" id="1Wd-Ls-ASv"/>
79100
<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"/>
101+
<constraint firstItem="hsr-oB-V4F" firstAttribute="leading" secondItem="kh9-bI-dsS" secondAttribute="leadingMargin" id="4nW-J5-0mh"/>
102+
<constraint firstItem="cye-0H-NTU" firstAttribute="centerY" secondItem="hsr-oB-V4F" secondAttribute="centerY" id="9q3-nv-XpF"/>
103+
<constraint firstItem="cye-0H-NTU" firstAttribute="trailing" secondItem="n2S-kO-F6i" secondAttribute="trailing" id="B06-F6-Yjn"/>
81104
<constraint firstItem="WHI-2I-FcN" firstAttribute="width" secondItem="kh9-bI-dsS" secondAttribute="width" id="BQz-L6-pQX"/>
105+
<constraint firstItem="gE4-qu-Fbg" firstAttribute="leading" secondItem="kh9-bI-dsS" secondAttribute="leadingMargin" id="C2y-IK-33N"/>
82106
<constraint firstAttribute="trailingMargin" secondItem="9aL-bC-GCL" secondAttribute="trailing" id="C6x-nZ-IJW"/>
107+
<constraint firstItem="hsr-oB-V4F" firstAttribute="top" secondItem="gE4-qu-Fbg" secondAttribute="bottom" constant="15" id="FEW-r5-nD2"/>
108+
<constraint firstAttribute="trailingMargin" relation="greaterThanOrEqual" secondItem="L4w-pG-YSK" secondAttribute="trailing" constant="7" id="JuE-3v-kmg"/>
109+
<constraint firstItem="L4w-pG-YSK" firstAttribute="centerY" secondItem="n2S-kO-F6i" secondAttribute="centerY" id="KnJ-r3-IaO"/>
83110
<constraint firstItem="WHI-2I-FcN" firstAttribute="top" secondItem="9aL-bC-GCL" secondAttribute="bottom" constant="8" id="KoQ-kH-Qqy"/>
84111
<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"/>
112+
<constraint firstItem="L4w-pG-YSK" firstAttribute="leading" secondItem="n2S-kO-F6i" secondAttribute="trailing" constant="8" id="Xlg-8v-Izd"/>
113+
<constraint firstItem="cye-0H-NTU" firstAttribute="leading" relation="greaterThanOrEqual" secondItem="hsr-oB-V4F" secondAttribute="trailing" constant="4" id="dDG-3A-47S"/>
114+
<constraint firstItem="n2S-kO-F6i" firstAttribute="centerY" secondItem="gE4-qu-Fbg" secondAttribute="centerY" id="g3d-Et-AI6"/>
115+
<constraint firstItem="n2S-kO-F6i" firstAttribute="leading" secondItem="gE4-qu-Fbg" secondAttribute="trailing" constant="8" id="jOe-kH-kiv"/>
86116
<constraint firstItem="gE4-qu-Fbg" firstAttribute="top" secondItem="jyV-Pf-zRb" secondAttribute="bottom" constant="8" id="nN1-xu-vOF"/>
87117
<constraint firstAttribute="bottom" secondItem="WHI-2I-FcN" secondAttribute="bottom" id="qxc-6B-GgV"/>
88118
</constraints>
89119
</view>
90120
<extendedEdge key="edgesForExtendedLayout" bottom="YES"/>
91121
<navigationItem key="navigationItem" id="xRd-lz-Ipx"/>
122+
<connections>
123+
<outlet property="changeContentButton" destination="L4w-pG-YSK" id="uJ3-lV-Qhe"/>
124+
<outlet property="presentInDrawerButton" destination="n2S-kO-F6i" id="duv-W5-jQx"/>
125+
<outlet property="pushButton" destination="gE4-qu-Fbg" id="Ahy-0R-7iY"/>
126+
<outlet property="useMiddlePositionSwitch" destination="cye-0H-NTU" id="7ww-jB-jah"/>
127+
</connections>
92128
</viewController>
93129
<placeholder placeholderIdentifier="IBFirstResponder" id="x5A-6p-PRh" sceneMemberID="firstResponder"/>
94130
</objects>
95-
<point key="canvasLocation" x="536.25" y="85.563380281690144"/>
131+
<point key="canvasLocation" x="536.25" y="85"/>
96132
</scene>
97133
</scenes>
98134
<resources>

Example/UIViewController-DisplayInDrawer/Content.storyboard

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@
7676
</view>
7777
<connections>
7878
<outlet property="closeButton" destination="rec-5d-zRb" id="ixl-JT-IQH"/>
79+
<outlet property="imageViewWidthConstraint" destination="kKl-Y0-BiY" id="uOH-KB-csx"/>
7980
<outlet property="panGestureRecognizer" destination="fqM-oh-g3U" id="jP4-TS-SFx"/>
8081
<outlet property="separatorView" destination="wOb-Gp-hc1" id="tlM-G0-l8m"/>
8182
<outlet property="textView" destination="44Q-NB-NbD" id="Fgd-gZ-BMe"/>

Example/UIViewController-DisplayInDrawer/ContentViewController.swift

Lines changed: 49 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 {
@@ -26,6 +26,7 @@ class ContentViewController: UIViewController {
2626
@IBOutlet private weak var closeButton: UIButton!
2727
@IBOutlet private weak var separatorView: UIView!
2828
@IBOutlet private weak var textView: UITextView!
29+
@IBOutlet private weak var imageViewWidthConstraint: NSLayoutConstraint!
2930
@IBOutlet private var panGestureRecognizer: UIPanGestureRecognizer!
3031
private var panGestureTarget: Any? //You are required to keep a strong reference of it.
3132
private var mode: ContentMode!
@@ -43,6 +44,7 @@ class ContentViewController: UIViewController {
4344

4445
override func viewDidLoad() {
4546
super.viewDidLoad()
47+
textView.text = defaultText
4648
setupMode()
4749
}
4850

@@ -57,14 +59,40 @@ class ContentViewController: UIViewController {
5759
setupMode()
5860
}
5961

60-
@IBAction private func dismiss() {
62+
func changeContent() {
63+
toggleText()
64+
toggleImage()
65+
}
66+
67+
@IBAction func dismiss() {
6168
drawerDismissClosure?()
6269
}
6370

6471
private func setupMode() {
6572
view.backgroundColor = mode.config.backgroundColor
6673
closeButton.isHidden = mode.config.isCloseButtonHidden
6774
}
75+
76+
private let defaultText = "DEFAULT TEXT: Lorem ipsum dolor sit er elit lamet, consectetaur cillium adipisicing pecu, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Nam liber te conscient to factor tum poen legum odioque civiuda."
77+
private let defaultImageViewWidthConstant: CGFloat = 0
78+
private let shorterText = "SHORTER TEXT: Lorem ipsum dolor sit er elit lamet, consectetaur cillium adipisicing pecu, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat"
79+
private let smallerImageviewWidthConstant: CGFloat = -50
80+
81+
private func toggleImage() {
82+
if imageViewWidthConstraint.constant == defaultImageViewWidthConstant {
83+
imageViewWidthConstraint.constant = smallerImageviewWidthConstant
84+
} else {
85+
imageViewWidthConstraint.constant = defaultImageViewWidthConstant
86+
}
87+
}
88+
89+
private func toggleText() {
90+
if textView.text == defaultText {
91+
textView.text = shorterText
92+
} else {
93+
textView.text = defaultText
94+
}
95+
}
6896
}
6997

7098
extension ContentViewController: DrawerConfiguration {
@@ -81,13 +109,17 @@ extension ContentViewController: DrawerConfiguration {
81109
}
82110

83111
func middlePositionY(for parentHeight: CGFloat) -> CGFloat? {
84-
guard isViewLoaded else { return nil }
85-
return parentHeight - separatorView.frame.minY
112+
guard isViewLoaded, useMiddlePosition else { return nil }
113+
return halfOpenPositionY(for: parentHeight)
86114
}
87115

88116
func bottomPositionY(for parentHeight: CGFloat) -> CGFloat {
89117
guard isViewLoaded else { return 0 }
90-
return parentHeight - 18
118+
if useMiddlePosition {
119+
return parentHeight - 18
120+
} else {
121+
return halfOpenPositionY(for: parentHeight)
122+
}
91123
}
92124

93125
func setPanGestureTarget(_ target: Any, action: Selector) {
@@ -101,6 +133,18 @@ extension ContentViewController: DrawerConfiguration {
101133
let bottomPadding: CGFloat = 16
102134
return fixedContentHeight + scrollableContentHeight + bottomPadding
103135
}
136+
137+
private var useMiddlePosition: Bool {
138+
guard let mode = mode,
139+
case let ContentMode.drawer(useMiddlePosition) = mode else {
140+
return false
141+
}
142+
return useMiddlePosition
143+
}
144+
145+
private func halfOpenPositionY(for parentHeight: CGFloat) -> CGFloat {
146+
return parentHeight - separatorView.frame.minY
147+
}
104148
}
105149

106150
extension UITextView {

Example/UIViewController-DisplayInDrawer/MainViewController.swift

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

66
class MainViewController: UIViewController {
7+
@IBOutlet weak var changeContentButton: UIButton!
8+
@IBOutlet weak var useMiddlePositionSwitch: UISwitch!
9+
@IBOutlet weak var presentInDrawerButton: UIButton!
10+
@IBOutlet weak var pushButton: UIButton!
11+
12+
weak var drawerContentController: ContentViewController? {
13+
didSet {
14+
refreshButtons()
15+
}
16+
}
17+
718
@IBAction func push(_ sender: Any) {
819
let controller = makeContentViewController()
920
controller.setup(for: .fullScreen)
@@ -12,15 +23,36 @@ class MainViewController: UIViewController {
1223

1324
@IBAction func presentInDrawer(_ sender: Any) {
1425
let controller = makeContentViewController()
15-
controller.setup(for: .drawer)
26+
controller.setup(for: .drawer(useMiddlePosition: useMiddlePositionSwitch.isOn))
1627
navigationController?.displayInDrawer(controller, drawerPositionDelegate: self)
28+
drawerContentController = controller
1729
}
1830

1931
private func makeContentViewController() -> ContentViewController {
2032
let storyboard = UIStoryboard(name: "Content", bundle: nil)
2133
let result = storyboard.instantiateInitialViewController() as! ContentViewController
2234
return result
2335
}
36+
37+
@IBAction func useMiddlePositionSwitchDidChangeValue(_ sender: Any) {
38+
drawerContentController?.dismiss()
39+
}
40+
41+
@IBAction func changeContentButtonPressed(_ sender: Any) {
42+
drawerContentController?.changeContent()
43+
}
44+
45+
private func refreshButtons() {
46+
if drawerContentController != nil {
47+
changeContentButton.isEnabled = true
48+
presentInDrawerButton.isEnabled = false
49+
pushButton.isEnabled = false
50+
} else {
51+
changeContentButton.isEnabled = false
52+
presentInDrawerButton.isEnabled = true
53+
pushButton.isEnabled = true
54+
}
55+
}
2456
}
2557

2658
extension MainViewController: DrawerPositionDelegate {
@@ -41,6 +73,10 @@ extension MainViewController: DrawerPositionDelegate {
4173
}
4274

4375
func didDismissDrawer() {
76+
// didSet is not called when ARC releases weak property, we must force it manually. Moreover we have to wait for ARC to kick in.
77+
DispatchQueue.main.async {
78+
self.refreshButtons()
79+
}
4480
NSLog("did dismiss drawer")
4581
}
4682
}

0 commit comments

Comments
 (0)