Skip to content

Commit e3007f6

Browse files
committed
Show a text field with the keyboard dismiss accessory view
1 parent 32e51d4 commit e3007f6

3 files changed

Lines changed: 71 additions & 2 deletions

File tree

Example/Example.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
/* Begin PBXBuildFile section */
1010
154CE52873EEBDF8EF05A61F /* Pods_Example.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D0DB1D3E89AB75183DB104E0 /* Pods_Example.framework */; };
1111
B548C5A71C8D55A8009D5AEE /* InfoPlist.strings in Resources */ = {isa = PBXBuildFile; fileRef = B548C5A51C8D55A8009D5AEE /* InfoPlist.strings */; };
12+
B548C5A91C8D6150009D5AEE /* ExampleCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = B548C5A81C8D6150009D5AEE /* ExampleCell.swift */; };
1213
B5E9F8FF1C8D3B6E00443DC7 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5E9F8FE1C8D3B6E00443DC7 /* AppDelegate.swift */; };
1314
B5E9F9011C8D3B6E00443DC7 /* ExampleViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5E9F9001C8D3B6E00443DC7 /* ExampleViewController.swift */; };
1415
B5E9F9061C8D3B6E00443DC7 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = B5E9F9051C8D3B6E00443DC7 /* Assets.xcassets */; };
@@ -17,6 +18,7 @@
1718

1819
/* Begin PBXFileReference section */
1920
B548C5A61C8D55A8009D5AEE /* Base */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = Base; path = Base.lproj/InfoPlist.strings; sourceTree = "<group>"; };
21+
B548C5A81C8D6150009D5AEE /* ExampleCell.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ExampleCell.swift; sourceTree = "<group>"; };
2022
B5E9F8FB1C8D3B6E00443DC7 /* Example.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Example.app; sourceTree = BUILT_PRODUCTS_DIR; };
2123
B5E9F8FE1C8D3B6E00443DC7 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
2224
B5E9F9001C8D3B6E00443DC7 /* ExampleViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ExampleViewController.swift; sourceTree = "<group>"; };
@@ -80,6 +82,7 @@
8082
children = (
8183
B5E9F8FE1C8D3B6E00443DC7 /* AppDelegate.swift */,
8284
B5E9F9051C8D3B6E00443DC7 /* Assets.xcassets */,
85+
B548C5A81C8D6150009D5AEE /* ExampleCell.swift */,
8386
B5E9F9001C8D3B6E00443DC7 /* ExampleViewController.swift */,
8487
B5E9F90A1C8D3B6E00443DC7 /* Info.plist */,
8588
B548C5A51C8D55A8009D5AEE /* InfoPlist.strings */,
@@ -211,6 +214,7 @@
211214
buildActionMask = 2147483647;
212215
files = (
213216
B5E9F8FF1C8D3B6E00443DC7 /* AppDelegate.swift in Sources */,
217+
B548C5A91C8D6150009D5AEE /* ExampleCell.swift in Sources */,
214218
B5E9F9011C8D3B6E00443DC7 /* ExampleViewController.swift in Sources */,
215219
);
216220
runOnlyForDeploymentPostprocessing = 0;

Example/Example/ExampleCell.swift

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
//
2+
// ExampleCell.swift
3+
// Example
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+
class ExampleCell: UITableViewCell {
30+
31+
var showcase: UIView? {
32+
didSet {
33+
oldValue?.removeFromSuperview()
34+
if let displayingView = showcase {
35+
contentView.addSubview(displayingView)
36+
displayingView.translatesAutoresizingMaskIntoConstraints = false
37+
38+
displayingView.topAnchor.constraintEqualToAnchor(contentView.topAnchor).active = true
39+
displayingView.leftAnchor.constraintEqualToAnchor(contentView.leftAnchor).active = true
40+
displayingView.bottomAnchor.constraintEqualToAnchor(contentView.bottomAnchor).active = true
41+
displayingView.rightAnchor.constraintEqualToAnchor(contentView.rightAnchor).active = true
42+
}
43+
}
44+
}
45+
46+
// MARK: - UITableViewCell
47+
48+
override func prepareForReuse() {
49+
super.prepareForReuse()
50+
showcase?.removeFromSuperview()
51+
showcase = nil
52+
}
53+
54+
}

Example/Example/ExampleViewController.swift

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class ExampleViewController: UIViewController, UITableViewDataSource {
3131

3232
private(set) lazy var tableView: UITableView = {
3333
let _tableView = UITableView(frame: .zero, style: .Grouped)
34-
_tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: NSStringFromClass(UITableViewCell.self))
34+
_tableView.registerClass(ExampleCell.self, forCellReuseIdentifier: NSStringFromClass(ExampleCell.self))
3535
_tableView.allowsSelection = false
3636
_tableView.dataSource = self
3737
return _tableView
@@ -70,7 +70,18 @@ class ExampleViewController: UIViewController, UITableViewDataSource {
7070
}
7171

7272
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
73-
let cell = tableView.dequeueReusableCellWithIdentifier(NSStringFromClass(UITableViewCell.self), forIndexPath: indexPath)
73+
let cell = tableView.dequeueReusableCellWithIdentifier(NSStringFromClass(ExampleCell.self), forIndexPath: indexPath)
74+
switch types[indexPath.section] {
75+
case is ICKeyboardDismissAccessoryView.Type:
76+
let textField = UITextField()
77+
textField.leftViewMode = .Always
78+
textField.leftView = UIView(frame: CGRect(x: 0, y: 0, width: 15, height: 15))
79+
textField.inputAccessoryView = ICKeyboardDismissAccessoryView()
80+
textField.placeholder = String(ICKeyboardDismissAccessoryView.self)
81+
(cell as? ExampleCell)?.showcase = textField
82+
default:
83+
break
84+
}
7485
return cell
7586
}
7687

0 commit comments

Comments
 (0)