Skip to content

Commit 54a3cd4

Browse files
committed
DEMO APP: Show dynamic drawer content change
1 parent 4d3fe42 commit 54a3cd4

File tree

4 files changed

+77
-3
lines changed

4 files changed

+77
-3
lines changed

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

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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>
@@ -84,6 +84,16 @@
8484
<nil key="textColor"/>
8585
<nil key="highlightedColor"/>
8686
</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>
8797
</subviews>
8898
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
8999
<constraints>
@@ -95,8 +105,11 @@
95105
<constraint firstItem="gE4-qu-Fbg" firstAttribute="leading" secondItem="kh9-bI-dsS" secondAttribute="leadingMargin" id="C2y-IK-33N"/>
96106
<constraint firstAttribute="trailingMargin" secondItem="9aL-bC-GCL" secondAttribute="trailing" id="C6x-nZ-IJW"/>
97107
<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"/>
98110
<constraint firstItem="WHI-2I-FcN" firstAttribute="top" secondItem="9aL-bC-GCL" secondAttribute="bottom" constant="8" id="KoQ-kH-Qqy"/>
99111
<constraint firstItem="WHI-2I-FcN" firstAttribute="centerX" secondItem="kh9-bI-dsS" secondAttribute="centerX" id="L9z-XR-MVL"/>
112+
<constraint firstItem="L4w-pG-YSK" firstAttribute="leading" secondItem="n2S-kO-F6i" secondAttribute="trailing" constant="8" id="Xlg-8v-Izd"/>
100113
<constraint firstItem="cye-0H-NTU" firstAttribute="leading" relation="greaterThanOrEqual" secondItem="hsr-oB-V4F" secondAttribute="trailing" constant="4" id="dDG-3A-47S"/>
101114
<constraint firstItem="n2S-kO-F6i" firstAttribute="centerY" secondItem="gE4-qu-Fbg" secondAttribute="centerY" id="g3d-Et-AI6"/>
102115
<constraint firstItem="n2S-kO-F6i" firstAttribute="leading" secondItem="gE4-qu-Fbg" secondAttribute="trailing" constant="8" id="jOe-kH-kiv"/>
@@ -107,12 +120,15 @@
107120
<extendedEdge key="edgesForExtendedLayout" bottom="YES"/>
108121
<navigationItem key="navigationItem" id="xRd-lz-Ipx"/>
109122
<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"/>
110126
<outlet property="useMiddlePositionSwitch" destination="cye-0H-NTU" id="7ww-jB-jah"/>
111127
</connections>
112128
</viewController>
113129
<placeholder placeholderIdentifier="IBFirstResponder" id="x5A-6p-PRh" sceneMemberID="firstResponder"/>
114130
</objects>
115-
<point key="canvasLocation" x="536.25" y="85.563380281690144"/>
131+
<point key="canvasLocation" x="536.25" y="85"/>
116132
</scene>
117133
</scenes>
118134
<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: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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,6 +59,11 @@ class ContentViewController: UIViewController {
5759
setupMode()
5860
}
5961

62+
func changeContent() {
63+
toggleText()
64+
toggleImage()
65+
}
66+
6067
@IBAction func dismiss() {
6168
drawerDismissClosure?()
6269
}
@@ -65,6 +72,27 @@ class ContentViewController: UIViewController {
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 {

Example/UIViewController-DisplayInDrawer/MainViewController.swift

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

66
class MainViewController: UIViewController {
7+
@IBOutlet weak var changeContentButton: UIButton!
78
@IBOutlet weak var useMiddlePositionSwitch: UISwitch!
8-
weak var drawerContentController: ContentViewController?
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+
918
@IBAction func push(_ sender: Any) {
1019
let controller = makeContentViewController()
1120
controller.setup(for: .fullScreen)
@@ -28,6 +37,22 @@ class MainViewController: UIViewController {
2837
@IBAction func useMiddlePositionSwitchDidChangeValue(_ sender: Any) {
2938
drawerContentController?.dismiss()
3039
}
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+
}
3156
}
3257

3358
extension MainViewController: DrawerPositionDelegate {
@@ -48,6 +73,10 @@ extension MainViewController: DrawerPositionDelegate {
4873
}
4974

5075
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+
}
5180
NSLog("did dismiss drawer")
5281
}
5382
}

0 commit comments

Comments
 (0)