Skip to content

Commit 317c66d

Browse files
committed
fix: support formSheet modal use case
1 parent b14abc2 commit 317c66d

File tree

5 files changed

+240
-28
lines changed

5 files changed

+240
-28
lines changed

KeyboardLayoutGuideExample/KeyboardLayoutGuideExample.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
/* Begin PBXBuildFile section */
1010
50F1DDE31FCC267900600110 /* Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 50F1DDE21FCC267900600110 /* Extensions.swift */; };
11+
8A6591BB290B479B0093D636 /* FormViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8A6591BA290B479B0093D636 /* FormViewController.swift */; };
1112
9902DE341FBB2659009E0D48 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9902DE331FBB2659009E0D48 /* AppDelegate.swift */; };
1213
9902DE361FBB2659009E0D48 /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9902DE351FBB2659009E0D48 /* ViewController.swift */; };
1314
9902DE391FBB2659009E0D48 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 9902DE371FBB2659009E0D48 /* Main.storyboard */; };
@@ -31,6 +32,7 @@
3132

3233
/* Begin PBXFileReference section */
3334
50F1DDE21FCC267900600110 /* Extensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Extensions.swift; sourceTree = "<group>"; };
35+
8A6591BA290B479B0093D636 /* FormViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FormViewController.swift; sourceTree = "<group>"; };
3436
9902DE301FBB2659009E0D48 /* KeyboardLayoutGuideExample.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = KeyboardLayoutGuideExample.app; sourceTree = BUILT_PRODUCTS_DIR; };
3537
9902DE331FBB2659009E0D48 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
3638
9902DE351FBB2659009E0D48 /* ViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewController.swift; sourceTree = "<group>"; };
@@ -76,6 +78,7 @@
7678
children = (
7779
9902DE331FBB2659009E0D48 /* AppDelegate.swift */,
7880
9902DE351FBB2659009E0D48 /* ViewController.swift */,
81+
8A6591BA290B479B0093D636 /* FormViewController.swift */,
7982
50F1DDE21FCC267900600110 /* Extensions.swift */,
8083
9902DE371FBB2659009E0D48 /* Main.storyboard */,
8184
9902DE3A1FBB2659009E0D48 /* Assets.xcassets */,
@@ -172,6 +175,7 @@
172175
files = (
173176
9902DE361FBB2659009E0D48 /* ViewController.swift in Sources */,
174177
9902DE341FBB2659009E0D48 /* AppDelegate.swift in Sources */,
178+
8A6591BB290B479B0093D636 /* FormViewController.swift in Sources */,
175179
50F1DDE31FCC267900600110 /* Extensions.swift in Sources */,
176180
);
177181
runOnlyForDeploymentPostprocessing = 0;

KeyboardLayoutGuideExample/KeyboardLayoutGuideExample/Base.lproj/Main.storyboard

Lines changed: 127 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="13529" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="BYZ-38-t0r">
3-
<device id="retina4_7" orientation="portrait">
4-
<adaptation id="fullscreen"/>
5-
</device>
2+
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="21225" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="BYZ-38-t0r">
3+
<device id="retina4_7" orientation="portrait" appearance="light"/>
64
<dependencies>
7-
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="13527"/>
5+
<deployment identifier="iOS"/>
6+
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="21207"/>
87
<capability name="Safe area layout guides" minToolsVersion="9.0"/>
8+
<capability name="System colors in document resources" minToolsVersion="11.0"/>
99
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
1010
</dependencies>
1111
<scenes>
@@ -18,11 +18,10 @@
1818
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
1919
<subviews>
2020
<textField opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="left" contentVerticalAlignment="center" borderStyle="roundedRect" placeholder="Try me!" textAlignment="natural" minimumFontSize="17" translatesAutoresizingMaskIntoConstraints="NO" id="fx8-LQ-Nzs">
21-
<rect key="frame" x="16" y="175" width="343" height="40"/>
21+
<rect key="frame" x="16" y="155" width="343" height="40"/>
2222
<constraints>
2323
<constraint firstAttribute="height" constant="40" id="DGk-vX-h2V"/>
2424
</constraints>
25-
<nil key="textColor"/>
2625
<fontDescription key="fontDescription" type="system" pointSize="16"/>
2726
<textInputTraits key="textInputTraits"/>
2827
<connections>
@@ -41,18 +40,28 @@
4140
<action selector="buttonTapped:" destination="BYZ-38-t0r" eventType="touchUpInside" id="mKh-YQ-EPx"/>
4241
</connections>
4342
</button>
43+
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="system" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="Xvr-jh-v7u">
44+
<rect key="frame" x="112" y="254" width="151" height="35"/>
45+
<state key="normal" title="Button"/>
46+
<buttonConfiguration key="configuration" style="plain" title="Show formSheet"/>
47+
<connections>
48+
<action selector="showFormSheet:" destination="BYZ-38-t0r" eventType="touchUpInside" id="DU6-7E-On0"/>
49+
</connections>
50+
</button>
4451
</subviews>
52+
<viewLayoutGuide key="safeArea" id="6Tk-OE-BBY"/>
4553
<color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
4654
<gestureRecognizers/>
4755
<constraints>
56+
<constraint firstItem="Xvr-jh-v7u" firstAttribute="centerX" secondItem="6Tk-OE-BBY" secondAttribute="centerX" id="0jd-Dq-OO0"/>
4857
<constraint firstItem="HGh-qK-wBn" firstAttribute="leading" secondItem="6Tk-OE-BBY" secondAttribute="leading" id="7n9-b9-wjj"/>
4958
<constraint firstItem="6Tk-OE-BBY" firstAttribute="trailing" secondItem="fx8-LQ-Nzs" secondAttribute="trailing" constant="16" id="Bfi-Vl-BMd"/>
5059
<constraint firstItem="HGh-qK-wBn" firstAttribute="trailing" secondItem="6Tk-OE-BBY" secondAttribute="trailing" id="Eoh-ng-2SN"/>
60+
<constraint firstItem="Xvr-jh-v7u" firstAttribute="top" secondItem="fx8-LQ-Nzs" secondAttribute="bottom" constant="59" id="cjs-UF-kkv"/>
5161
<constraint firstItem="fx8-LQ-Nzs" firstAttribute="top" secondItem="6Tk-OE-BBY" secondAttribute="top" constant="155" id="gy3-0T-4wY"/>
5262
<constraint firstItem="fx8-LQ-Nzs" firstAttribute="leading" secondItem="6Tk-OE-BBY" secondAttribute="leading" constant="16" id="mOg-sM-jRr"/>
5363
<constraint firstItem="6Tk-OE-BBY" firstAttribute="bottom" secondItem="HGh-qK-wBn" secondAttribute="bottom" placeholder="YES" id="xEd-PU-g0M"/>
5464
</constraints>
55-
<viewLayoutGuide key="safeArea" id="6Tk-OE-BBY"/>
5665
</view>
5766
<connections>
5867
<outlet property="button" destination="HGh-qK-wBn" id="euq-Bl-o3t"/>
@@ -62,5 +71,115 @@
6271
</objects>
6372
<point key="canvasLocation" x="84" y="133.5832083958021"/>
6473
</scene>
74+
<!--Form View Controller-->
75+
<scene sceneID="tC6-MQ-afx">
76+
<objects>
77+
<viewController storyboardIdentifier="FormViewController" id="bJ8-5v-31W" customClass="FormViewController" customModule="KeyboardLayoutGuideExample" customModuleProvider="target" sceneMemberID="viewController">
78+
<view key="view" contentMode="scaleToFill" id="IXq-8S-3Cs">
79+
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
80+
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
81+
<subviews>
82+
<stackView opaque="NO" contentMode="scaleToFill" axis="vertical" distribution="fillEqually" translatesAutoresizingMaskIntoConstraints="NO" id="Ll9-Ds-wFg">
83+
<rect key="frame" x="67.5" y="208.5" width="240" height="250"/>
84+
<subviews>
85+
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="Vh4-2f-6dw">
86+
<rect key="frame" x="0.0" y="0.0" width="240" height="50"/>
87+
<subviews>
88+
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Pan here!" textAlignment="center" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="GGd-Nc-OyI">
89+
<rect key="frame" x="0.0" y="12" width="240" height="26.5"/>
90+
<fontDescription key="fontDescription" type="system" weight="semibold" pointSize="22"/>
91+
<color key="textColor" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
92+
<nil key="highlightedColor"/>
93+
</label>
94+
</subviews>
95+
<color key="backgroundColor" systemColor="systemMintColor"/>
96+
<gestureRecognizers/>
97+
<constraints>
98+
<constraint firstItem="GGd-Nc-OyI" firstAttribute="leading" secondItem="Vh4-2f-6dw" secondAttribute="leading" id="CIZ-Wa-Fes"/>
99+
<constraint firstItem="GGd-Nc-OyI" firstAttribute="centerY" secondItem="Vh4-2f-6dw" secondAttribute="centerY" id="Kf5-7l-69B"/>
100+
<constraint firstAttribute="height" constant="50" id="NX0-Y5-xxb"/>
101+
<constraint firstAttribute="trailing" secondItem="GGd-Nc-OyI" secondAttribute="trailing" id="hzQ-0W-mzF"/>
102+
</constraints>
103+
<connections>
104+
<outletCollection property="gestureRecognizers" destination="W0e-bo-xwl" appends="YES" id="FQD-YW-ov3"/>
105+
</connections>
106+
</view>
107+
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="QDv-J8-giE">
108+
<rect key="frame" x="0.0" y="50" width="240" height="50"/>
109+
<color key="backgroundColor" systemColor="systemOrangeColor"/>
110+
</view>
111+
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="rge-S7-9pD">
112+
<rect key="frame" x="0.0" y="100" width="240" height="50"/>
113+
<color key="backgroundColor" systemColor="systemPinkColor"/>
114+
</view>
115+
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="cnG-cE-MLW">
116+
<rect key="frame" x="0.0" y="150" width="240" height="50"/>
117+
<color key="backgroundColor" systemColor="systemPurpleColor"/>
118+
</view>
119+
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="0YU-jj-Slu">
120+
<rect key="frame" x="0.0" y="200" width="240" height="50"/>
121+
<subviews>
122+
<textField opaque="NO" contentMode="scaleToFill" horizontalHuggingPriority="248" contentHorizontalAlignment="left" contentVerticalAlignment="center" borderStyle="roundedRect" textAlignment="natural" minimumFontSize="17" translatesAutoresizingMaskIntoConstraints="NO" id="HDd-Ld-Xq7">
123+
<rect key="frame" x="0.0" y="0.0" width="240" height="50"/>
124+
<fontDescription key="fontDescription" type="system" pointSize="14"/>
125+
<textInputTraits key="textInputTraits"/>
126+
</textField>
127+
</subviews>
128+
<color key="backgroundColor" systemColor="systemBrownColor"/>
129+
<constraints>
130+
<constraint firstItem="HDd-Ld-Xq7" firstAttribute="leading" secondItem="0YU-jj-Slu" secondAttribute="leading" id="8zh-Qi-VNM"/>
131+
<constraint firstAttribute="trailing" secondItem="HDd-Ld-Xq7" secondAttribute="trailing" id="Dij-BX-cmn"/>
132+
<constraint firstAttribute="bottom" secondItem="HDd-Ld-Xq7" secondAttribute="bottom" id="GMB-gJ-cyj"/>
133+
<constraint firstItem="HDd-Ld-Xq7" firstAttribute="top" secondItem="0YU-jj-Slu" secondAttribute="top" id="MWi-Dk-sKa"/>
134+
</constraints>
135+
</view>
136+
</subviews>
137+
<constraints>
138+
<constraint firstAttribute="width" constant="240" id="sE6-vF-ZVl"/>
139+
</constraints>
140+
</stackView>
141+
</subviews>
142+
<viewLayoutGuide key="safeArea" id="WGo-Vq-oFg"/>
143+
<color key="backgroundColor" systemColor="systemBackgroundColor"/>
144+
<constraints>
145+
<constraint firstItem="Ll9-Ds-wFg" firstAttribute="centerY" secondItem="WGo-Vq-oFg" secondAttribute="centerY" priority="250" id="FLT-9w-rlP"/>
146+
<constraint firstItem="Ll9-Ds-wFg" firstAttribute="centerX" secondItem="WGo-Vq-oFg" secondAttribute="centerX" id="i3K-fV-5T9"/>
147+
</constraints>
148+
</view>
149+
<connections>
150+
<outlet property="elementHeight" destination="NX0-Y5-xxb" id="WbI-dP-dad"/>
151+
<outlet property="textField" destination="HDd-Ld-Xq7" id="aLj-8j-HxN"/>
152+
<outlet property="vStack" destination="Ll9-Ds-wFg" id="Dd6-dW-cSv"/>
153+
</connections>
154+
</viewController>
155+
<placeholder placeholderIdentifier="IBFirstResponder" id="Gej-c5-SgE" userLabel="First Responder" customClass="UIResponder" sceneMemberID="firstResponder"/>
156+
<panGestureRecognizer minimumNumberOfTouches="1" id="W0e-bo-xwl">
157+
<connections>
158+
<action selector="handlePan:" destination="bJ8-5v-31W" id="26R-6N-87s"/>
159+
</connections>
160+
</panGestureRecognizer>
161+
</objects>
162+
<point key="canvasLocation" x="765.60000000000002" y="133.5832083958021"/>
163+
</scene>
65164
</scenes>
165+
<resources>
166+
<systemColor name="systemBackgroundColor">
167+
<color white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
168+
</systemColor>
169+
<systemColor name="systemBrownColor">
170+
<color red="0.63529411764705879" green="0.51764705882352946" blue="0.36862745098039218" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
171+
</systemColor>
172+
<systemColor name="systemMintColor">
173+
<color red="0.0" green="0.7803921568627451" blue="0.74509803921568629" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
174+
</systemColor>
175+
<systemColor name="systemOrangeColor">
176+
<color red="1" green="0.58431372549019611" blue="0.0" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
177+
</systemColor>
178+
<systemColor name="systemPinkColor">
179+
<color red="1" green="0.17647058823529413" blue="0.33333333333333331" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
180+
</systemColor>
181+
<systemColor name="systemPurpleColor">
182+
<color red="0.68627450980392157" green="0.32156862745098042" blue="0.87058823529411766" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
183+
</systemColor>
184+
</resources>
66185
</document>
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import KeyboardLayoutGuide
2+
import UIKit
3+
4+
protocol FormViewControllerDelegate: AnyObject {
5+
func formViewControllerWillDismiss()
6+
}
7+
8+
final class FormViewController: UIViewController {
9+
10+
@IBOutlet weak var vStack: UIStackView!
11+
@IBOutlet weak var textField: UITextField!
12+
@IBOutlet weak var elementHeight: NSLayoutConstraint!
13+
private weak var delegate: FormViewControllerDelegate?
14+
15+
static func make(delegate: FormViewControllerDelegate) -> FormViewController {
16+
let vc = UIStoryboard(name: "Main", bundle: nil)
17+
.instantiateViewController(withIdentifier: "FormViewController") as! FormViewController
18+
vc.delegate = delegate
19+
vc.modalPresentationStyle = .formSheet
20+
vc.preferredContentSize = CGSize(width: 500, height: 600)
21+
return vc
22+
}
23+
24+
override func viewDidLoad() {
25+
super.viewDidLoad()
26+
27+
Keyboard.shared.presentedViewController = self
28+
vStack.bottomAnchor.constraint(lessThanOrEqualTo: view.keyboardLayoutGuideNoSafeArea.topAnchor).isActive = true
29+
30+
view.addGestureRecognizer(UITapGestureRecognizer(target: textField, action: #selector(resignFirstResponder)))
31+
}
32+
33+
override func viewWillDisappear(_ animated: Bool) {
34+
super.viewWillDisappear(animated)
35+
36+
delegate?.formViewControllerWillDismiss()
37+
}
38+
39+
@IBAction func handlePan(_ pan: UIPanGestureRecognizer) {
40+
elementHeight.constant = 50 + pan.translation(in: pan.view).y
41+
}
42+
43+
}

0 commit comments

Comments
 (0)