Skip to content

Commit 330cf5e

Browse files
committed
Add a keyboard dismiss text field
1 parent e3007f6 commit 330cf5e

File tree

3 files changed

+76
-7
lines changed

3 files changed

+76
-7
lines changed

Example/Example/ExampleViewController.swift

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class ExampleViewController: UIViewController, UITableViewDataSource {
3737
return _tableView
3838
}()
3939

40-
private let types: [UIView.Type] = [ICKeyboardDismissAccessoryView.self]
40+
private let types: [UIView.Type] = [ICKeyboardDismissTextField.self]
4141

4242
// MARK: - Initialization
4343

@@ -66,18 +66,22 @@ class ExampleViewController: UIViewController, UITableViewDataSource {
6666
}
6767

6868
func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
69-
return String(types[section])
69+
switch types[section] {
70+
case is ICKeyboardDismissTextField.Type:
71+
return "Dismiss Keyboard"
72+
default:
73+
return ""
74+
}
7075
}
7176

7277
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
7378
let cell = tableView.dequeueReusableCellWithIdentifier(NSStringFromClass(ExampleCell.self), forIndexPath: indexPath)
7479
switch types[indexPath.section] {
75-
case is ICKeyboardDismissAccessoryView.Type:
76-
let textField = UITextField()
80+
case let type as ICKeyboardDismissTextField.Type:
81+
let textField = type.init()
7782
textField.leftViewMode = .Always
7883
textField.leftView = UIView(frame: CGRect(x: 0, y: 0, width: 15, height: 15))
79-
textField.inputAccessoryView = ICKeyboardDismissAccessoryView()
80-
textField.placeholder = String(ICKeyboardDismissAccessoryView.self)
84+
textField.placeholder = String(type)
8185
(cell as? ExampleCell)?.showcase = textField
8286
default:
8387
break

Source/ICKeyboardDismissAccessoryView.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import UIKit
2828

2929
public class ICKeyboardDismissAccessoryView: UIView {
3030

31-
private(set) lazy var dismissButton: UIButton = {
31+
public private(set) lazy var dismissButton: UIButton = {
3232
let _button = UIButton()
3333
let resources = NSBundle(forClass: self.dynamicType)
3434
let icon = UIImage(named: "icook-iphone-button-hide-keyboard", inBundle: resources, compatibleWithTraitCollection: nil)
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
//
2+
// ICKeyboardDismissTextField.swift
3+
// ICInputAccessory
4+
//
5+
// Created by Ben on 07/03/2016.
6+
// Copyright © 2016 Polydice, Inc.
7+
//
8+
// Permission is hereby granted, free of charge, to any person obtaining a copy
9+
// of this software and associated documentation files (the "Software"), to deal
10+
// in the Software without restriction, including without limitation the rights
11+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
// copies of the Software, and to permit persons to whom the Software is
13+
// furnished to do so, subject to the following conditions:
14+
//
15+
// The above copyright notice and this permission notice shall be included in all
16+
// copies or substantial portions of the Software.
17+
//
18+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24+
// SOFTWARE.
25+
//
26+
27+
import UIKit
28+
29+
public class ICKeyboardDismissTextField: UITextField {
30+
31+
private lazy var accessoryView: UIView = {
32+
let _accessory = ICKeyboardDismissAccessoryView()
33+
_accessory.dismissButton.addTarget(self, action: Selector("dismiss:"), forControlEvents: .TouchUpInside)
34+
return _accessory
35+
}()
36+
37+
// MARK: - Initialization
38+
39+
override public init(frame: CGRect) {
40+
super.init(frame: frame)
41+
inputAccessoryView = accessoryView
42+
}
43+
44+
required public init?(coder aDecoder: NSCoder) {
45+
super.init(coder: aDecoder)
46+
inputAccessoryView = accessoryView
47+
}
48+
49+
// MARK: - UIResponder
50+
51+
override public func becomeFirstResponder() -> Bool {
52+
accessoryView.alpha = 1
53+
return super.becomeFirstResponder()
54+
}
55+
56+
// MARK: - Private Methods
57+
58+
@IBAction private func dismiss(sender: UIButton) {
59+
resignFirstResponder()
60+
UIView.animateWithDuration(0.3) {
61+
self.accessoryView.alpha = 0
62+
}
63+
}
64+
65+
}

0 commit comments

Comments
 (0)