Skip to content

Commit 07baabb

Browse files
authored
Fix Keyboard-Hiding and Various Refactors (#3)
* Fix Keyboard Avoiding for text fields * Separate code * Clean up Cartography * Add onCompletion block to show
1 parent e85ab0d commit 07baabb

10 files changed

Lines changed: 180 additions & 162 deletions

File tree

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>IDEDidComputeMac32BitWarning</key>
6+
<true/>
7+
</dict>
8+
</plist>

SimpleAlertExample/SimpleAlertExample/Assets.xcassets/AppIcon.appiconset/Contents.json

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
{
22
"images" : [
3+
{
4+
"idiom" : "iphone",
5+
"size" : "20x20",
6+
"scale" : "2x"
7+
},
8+
{
9+
"idiom" : "iphone",
10+
"size" : "20x20",
11+
"scale" : "3x"
12+
},
313
{
414
"idiom" : "iphone",
515
"size" : "29x29",
@@ -30,6 +40,16 @@
3040
"size" : "60x60",
3141
"scale" : "3x"
3242
},
43+
{
44+
"idiom" : "ipad",
45+
"size" : "20x20",
46+
"scale" : "1x"
47+
},
48+
{
49+
"idiom" : "ipad",
50+
"size" : "20x20",
51+
"scale" : "2x"
52+
},
3353
{
3454
"idiom" : "ipad",
3555
"size" : "29x29",
@@ -59,6 +79,16 @@
5979
"idiom" : "ipad",
6080
"size" : "76x76",
6181
"scale" : "2x"
82+
},
83+
{
84+
"idiom" : "ipad",
85+
"size" : "83.5x83.5",
86+
"scale" : "2x"
87+
},
88+
{
89+
"idiom" : "ios-marketing",
90+
"size" : "1024x1024",
91+
"scale" : "1x"
6292
}
6393
],
6494
"info" : {

SimpleAlertExample/SimpleAlertExample/Base.lproj/Main.storyboard

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1-
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
2-
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="10116" systemVersion="15E65" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" initialViewController="BYZ-38-t0r">
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="14109" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="BYZ-38-t0r">
3+
<device id="retina4_7" orientation="portrait">
4+
<adaptation id="fullscreen"/>
5+
</device>
36
<dependencies>
47
<deployment identifier="iOS"/>
5-
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="10085"/>
8+
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="14088"/>
9+
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
610
</dependencies>
711
<scenes>
812
<!--View Controller-->
@@ -14,18 +18,19 @@
1418
<viewControllerLayoutGuide type="bottom" id="wfy-db-euE"/>
1519
</layoutGuides>
1620
<view key="view" contentMode="scaleToFill" id="8bC-Xf-vdC">
17-
<rect key="frame" x="0.0" y="0.0" width="600" height="600"/>
21+
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
1822
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
1923
<subviews>
2024
<button opaque="NO" contentMode="scaleToFill" fixedFrame="YES" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="JVU-0X-qE5">
21-
<rect key="frame" x="20" y="20" width="98" height="30"/>
25+
<rect key="frame" x="5" y="5" width="98" height="30"/>
26+
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
2227
<state key="normal" title="Show Alert"/>
2328
<connections>
2429
<action selector="showFirstAlert" destination="BYZ-38-t0r" eventType="touchUpInside" id="T5u-69-Sz0"/>
2530
</connections>
2631
</button>
2732
</subviews>
28-
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="calibratedWhite"/>
33+
<color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
2934
</view>
3035
</viewController>
3136
<placeholder placeholderIdentifier="IBFirstResponder" id="dkx-z0-nzr" sceneMemberID="firstResponder"/>

SimpleAlertExample/SimpleAlertExample/Info.plist

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
<string>UIInterfaceOrientationPortrait</string>
3636
<string>UIInterfaceOrientationLandscapeLeft</string>
3737
<string>UIInterfaceOrientationLandscapeRight</string>
38+
<string>UIInterfaceOrientationPortraitUpsideDown</string>
3839
</array>
3940
<key>UISupportedInterfaceOrientations~ipad</key>
4041
<array>

SimpleAlertExample/SimpleAlertExample/ViewController.swift

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import SimpleAlertLib
1212
class ViewController: UIViewController {
1313

1414
override func viewDidAppear(_ animated: Bool) {
15-
self.view.backgroundColor = UIColor.orange
15+
self.view.backgroundColor = UIColor.gray
1616
showFirstAlert()
1717
for i in 1...5 {
1818
showThirdAlert(i)
@@ -31,34 +31,31 @@ class ViewController: UIViewController {
3131
alert.addButtonWithTitle("Another Alert, Dark") {
3232
self.showSecondAlert(false)
3333
}
34-
35-
alert.showInWindow(self.view.window!)
36-
37-
34+
alert.show(onComplete: {}, animated: true)
3835
}
3936

4037
func showSecondAlert(_ useLight: Bool) {
4138

4239
let alert = SimpleAlert.makeAlert("Another Alert", message: "You could fill out these boxes.");
43-
let username = alert.addTextFieldWithPlaceholder("Username", secureEntry: false, changeHandler: { (textField) in
40+
let username = alert.addTextField(with: "Username", secureEntry: false, changeHandler: { (textField) in
4441
print("typing!")
4542
})
46-
alert.addTextFieldWithPlaceholder("Pass", secureEntry: true, changeHandler: nil)
43+
alert.addTextField(with: "Pass", secureEntry: true, changeHandler: nil)
4744
alert.addButtonWithTitle("OK", block: {
4845
print("Okay pressed, username is: \(username.text!)")
4946
})
5047
alert.addButtonWithTitle("Cancel", block: {})
5148
alert.theme = useLight ? .light : .dark
52-
alert.showInWindow(self.view.window!)
53-
49+
alert.show(onComplete: {
50+
username.becomeFirstResponder()
51+
}, animated: true)
5452
}
5553

5654
func showThirdAlert(_ which: Int) {
5755
let alert = SimpleAlert.makeAlert(nil, message: "Many alerts: \(which)");
5856
alert.addButtonWithTitle("OK", block: {})
5957
alert.theme = .light
60-
alert.showInWindow(self.view.window!)
61-
58+
alert.show(onComplete: {}, animated: true)
6259
}
6360

6461

SimpleAlertLib.xcodeproj/project.pbxproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
3775F6C61D8B635B0056A88B /* Cartography.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 3775F6C51D8B635B0056A88B /* Cartography.framework */; };
1111
C99E90971CB95E14001E70CC /* SimpleAlertLib.h in Headers */ = {isa = PBXBuildFile; fileRef = C99E90961CB95E14001E70CC /* SimpleAlertLib.h */; settings = {ATTRIBUTES = (Public, ); }; };
1212
C99E90A91CB95E6B001E70CC /* SimpleAlert.swift in Sources */ = {isa = PBXBuildFile; fileRef = C99E90A81CB95E6B001E70CC /* SimpleAlert.swift */; };
13+
DBBAAB7520A5D14E005A13CB /* SimpleAlertButton.swift in Sources */ = {isa = PBXBuildFile; fileRef = DBBAAB7420A5D14E005A13CB /* SimpleAlertButton.swift */; };
14+
DBBAAB7820A5D36E005A13CB /* SimpleAlert+AvoidKeyboard.swift in Sources */ = {isa = PBXBuildFile; fileRef = DBBAAB7720A5D36E005A13CB /* SimpleAlert+AvoidKeyboard.swift */; };
1315
/* End PBXBuildFile section */
1416

1517
/* Begin PBXFileReference section */
@@ -18,6 +20,8 @@
1820
C99E90961CB95E14001E70CC /* SimpleAlertLib.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = SimpleAlertLib.h; sourceTree = "<group>"; };
1921
C99E90981CB95E14001E70CC /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
2022
C99E90A81CB95E6B001E70CC /* SimpleAlert.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SimpleAlert.swift; sourceTree = "<group>"; };
23+
DBBAAB7420A5D14E005A13CB /* SimpleAlertButton.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SimpleAlertButton.swift; sourceTree = "<group>"; };
24+
DBBAAB7720A5D36E005A13CB /* SimpleAlert+AvoidKeyboard.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "SimpleAlert+AvoidKeyboard.swift"; sourceTree = "<group>"; };
2125
/* End PBXFileReference section */
2226

2327
/* Begin PBXFrameworksBuildPhase section */
@@ -55,6 +59,8 @@
5559
C99E90961CB95E14001E70CC /* SimpleAlertLib.h */,
5660
C99E90981CB95E14001E70CC /* Info.plist */,
5761
C99E90A81CB95E6B001E70CC /* SimpleAlert.swift */,
62+
DBBAAB7720A5D36E005A13CB /* SimpleAlert+AvoidKeyboard.swift */,
63+
DBBAAB7420A5D14E005A13CB /* SimpleAlertButton.swift */,
5864
);
5965
path = SimpleAlertLib;
6066
sourceTree = "<group>";
@@ -138,7 +144,9 @@
138144
isa = PBXSourcesBuildPhase;
139145
buildActionMask = 2147483647;
140146
files = (
147+
DBBAAB7520A5D14E005A13CB /* SimpleAlertButton.swift in Sources */,
141148
C99E90A91CB95E6B001E70CC /* SimpleAlert.swift in Sources */,
149+
DBBAAB7820A5D36E005A13CB /* SimpleAlert+AvoidKeyboard.swift in Sources */,
142150
);
143151
runOnlyForDeploymentPostprocessing = 0;
144152
};
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import UIKit
2+
3+
extension SimpleAlert {
4+
func addKeyboardNotifications() {
5+
NotificationCenter.default.addObserver(self, selector: #selector(keyboardDidShow(_:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
6+
NotificationCenter.default.addObserver(self, selector: #selector(keyboardDidHide(_:)), name: NSNotification.Name.UIKeyboardDidHide, object: nil)
7+
}
8+
9+
func removeKeyboardNotifications() {
10+
NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardWillShow, object: nil)
11+
NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardWillHide, object: nil)
12+
}
13+
14+
@objc open func keyboardDidShow(_ notification: Notification) {
15+
constrainBox(toTopQuarter: true)
16+
animateForKeyboard(with: notification)
17+
}
18+
19+
@objc open func keyboardDidHide(_ notification: Notification) {
20+
constrainBox(toTopQuarter: showAlertInTopHalf)
21+
animateForKeyboard(with: notification)
22+
}
23+
24+
func animateForKeyboard(with notification: Notification) {
25+
let userInfo = notification.userInfo
26+
var animationOptions: UIViewAnimationOptions = []
27+
animationOptions.insert(.curveEaseInOut)
28+
29+
var animationDuration: TimeInterval = 0.0
30+
31+
if let animationDurationFromUserInfo = userInfo?[UIKeyboardAnimationDurationUserInfoKey] as? TimeInterval {
32+
animationDuration = animationDurationFromUserInfo
33+
}
34+
35+
UIView.animate(withDuration: animationDuration, delay: 0.0, options: animationOptions, animations: {
36+
self.layoutIfNeeded()
37+
})
38+
}
39+
}
40+

0 commit comments

Comments
 (0)