Skip to content

Commit e03af0d

Browse files
committed
Present the customized token field modally
1 parent 524d4fd commit e03af0d

8 files changed

Lines changed: 122 additions & 36 deletions

File tree

Example/Example.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
B548C5A71C8D55A8009D5AEE /* InfoPlist.strings in Resources */ = {isa = PBXBuildFile; fileRef = B548C5A51C8D55A8009D5AEE /* InfoPlist.strings */; };
1313
B548C5A91C8D6150009D5AEE /* ExampleCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = B548C5A81C8D6150009D5AEE /* ExampleCell.swift */; };
1414
B548C5C51C8E91B0009D5AEE /* ICInputAccessoryUITests.swift in Sources */ = {isa = PBXBuildFile; fileRef = B548C5C41C8E91B0009D5AEE /* ICInputAccessoryUITests.swift */; };
15+
B5C53E191C92851400AF3489 /* CustomizedTokenViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5C53E181C92851400AF3489 /* CustomizedTokenViewController.swift */; };
1516
B5E9F8FF1C8D3B6E00443DC7 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5E9F8FE1C8D3B6E00443DC7 /* AppDelegate.swift */; };
1617
B5E9F9011C8D3B6E00443DC7 /* ExampleViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5E9F9001C8D3B6E00443DC7 /* ExampleViewController.swift */; };
1718
B5E9F9061C8D3B6E00443DC7 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = B5E9F9051C8D3B6E00443DC7 /* Assets.xcassets */; };
@@ -35,6 +36,7 @@
3536
B548C5C21C8E91B0009D5AEE /* ICInputAccessoryUITests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = ICInputAccessoryUITests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
3637
B548C5C41C8E91B0009D5AEE /* ICInputAccessoryUITests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ICInputAccessoryUITests.swift; sourceTree = "<group>"; };
3738
B548C5C61C8E91B0009D5AEE /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
39+
B5C53E181C92851400AF3489 /* CustomizedTokenViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CustomizedTokenViewController.swift; sourceTree = "<group>"; };
3840
B5E9F8FB1C8D3B6E00443DC7 /* Example.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Example.app; sourceTree = BUILT_PRODUCTS_DIR; };
3941
B5E9F8FE1C8D3B6E00443DC7 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
4042
B5E9F9001C8D3B6E00443DC7 /* ExampleViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ExampleViewController.swift; sourceTree = "<group>"; };
@@ -117,6 +119,7 @@
117119
B5E9F8FE1C8D3B6E00443DC7 /* AppDelegate.swift */,
118120
B5E9F9051C8D3B6E00443DC7 /* Assets.xcassets */,
119121
B52819571C90215C007D01D5 /* CustomizedTokenField.swift */,
122+
B5C53E181C92851400AF3489 /* CustomizedTokenViewController.swift */,
120123
B548C5A81C8D6150009D5AEE /* ExampleCell.swift */,
121124
B5E9F9001C8D3B6E00443DC7 /* ExampleViewController.swift */,
122125
B5E9F90A1C8D3B6E00443DC7 /* Info.plist */,
@@ -303,6 +306,7 @@
303306
files = (
304307
B5E9F8FF1C8D3B6E00443DC7 /* AppDelegate.swift in Sources */,
305308
B52819581C90215C007D01D5 /* CustomizedTokenField.swift in Sources */,
309+
B5C53E191C92851400AF3489 /* CustomizedTokenViewController.swift in Sources */,
306310
B548C5A91C8D6150009D5AEE /* ExampleCell.swift in Sources */,
307311
B5E9F9011C8D3B6E00443DC7 /* ExampleViewController.swift in Sources */,
308312
);
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"info" : {
3+
"version" : 1,
4+
"author" : "xcode"
5+
}
6+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"images" : [
3+
{
4+
"idiom" : "universal",
5+
"filename" : "icook-iphone-input-search.pdf"
6+
}
7+
],
8+
"info" : {
9+
"version" : 1,
10+
"author" : "xcode"
11+
}
12+
}
Binary file not shown.

Example/Example/CustomizedTokenField.swift

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -27,29 +27,16 @@
2727
import UIKit
2828
import ICInputAccessory
2929

30-
class CustomizedTokenField: UIView {
30+
class CustomizedTokenField: ICTokenField {
3131

3232
override init(frame: CGRect) {
3333
super.init(frame: frame)
34-
setUpSubviews()
34+
applyCustomizedStyle()
3535
}
3636

3737
required init?(coder aDecoder: NSCoder) {
3838
super.init(coder: aDecoder)
39-
setUpSubviews()
40-
}
41-
42-
private func setUpSubviews() {
43-
backgroundColor = UIColor(red:0.96, green:0.48, blue:0.4, alpha:1)
44-
45-
let tokenField = ICTokenField()
46-
tokenField.applyCustomizedStyle()
47-
tokenField.translatesAutoresizingMaskIntoConstraints = false
48-
addSubview(tokenField)
49-
50-
let views = ["field": tokenField]
51-
addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-10-[field]-10-|", options: [], metrics: nil, views: views))
52-
addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-3-[field]-3-|", options: [], metrics: nil, views: views))
39+
applyCustomizedStyle()
5340
}
5441

5542
}
@@ -61,6 +48,8 @@ class CustomizedTokenField: UIView {
6148
extension ICTokenField {
6249

6350
func applyCustomizedStyle() {
51+
icon = UIImage(named: "icook-iphone-input-search")
52+
6453
layer.cornerRadius = 5
6554
layer.shouldRasterize = true
6655
layer.rasterizationScale = UIScreen.mainScreen().scale
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
//
2+
// CustomizedTokenViewController.swift
3+
// Example
4+
//
5+
// Created by Ben on 11/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+
import ICInputAccessory
29+
30+
class CustomizedTokenViewController: UIViewController {
31+
32+
private let tokenField = CustomizedTokenField()
33+
34+
// MARK: - UIViewController
35+
36+
override func loadView() {
37+
super.loadView()
38+
view.backgroundColor = UIColor.whiteColor()
39+
}
40+
41+
override func viewDidLoad() {
42+
super.viewDidLoad()
43+
navigationController?.navigationBar.barTintColor = UIColor(red:0.96, green:0.48, blue:0.4, alpha:1)
44+
navigationController?.navigationBar.translucent = false
45+
navigationController?.navigationBar.barStyle = .Black
46+
47+
let cancelBarButton = UIBarButtonItem(barButtonSystemItem: .Cancel, target: self, action: Selector("dismiss:"))
48+
cancelBarButton.tintColor = UIColor.whiteColor()
49+
navigationItem.rightBarButtonItem = cancelBarButton
50+
51+
navigationItem.titleView = tokenField
52+
}
53+
54+
override func viewWillAppear(animated: Bool) {
55+
super.viewWillAppear(animated)
56+
tokenField.becomeFirstResponder()
57+
}
58+
59+
override func viewWillDisappear(animated: Bool) {
60+
super.viewWillAppear(animated)
61+
tokenField.resignFirstResponder()
62+
}
63+
64+
// MARK: - UIResponder Callbacks
65+
66+
@IBAction private func dismiss(sender: UIBarButtonItem) {
67+
presentingViewController?.dismissViewControllerAnimated(true, completion: nil)
68+
}
69+
70+
}

Example/Example/ExampleCell.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ class ExampleCell: UITableViewCell {
5555
super.prepareForReuse()
5656
showcase?.removeFromSuperview()
5757
showcase = nil
58+
textLabel?.text = nil
59+
accessoryType = .None
5860
}
5961

6062
}

Example/Example/ExampleViewController.swift

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,7 @@
2727
import UIKit
2828
import ICInputAccessory
2929

30-
class ExampleViewController: UIViewController, UITableViewDataSource {
31-
32-
private(set) lazy var tableView: UITableView = {
33-
let _tableView = UITableView(frame: .zero, style: .Grouped)
34-
_tableView.registerClass(ExampleCell.self, forCellReuseIdentifier: NSStringFromClass(ExampleCell.self))
35-
_tableView.allowsSelection = false
36-
_tableView.dataSource = self
37-
return _tableView
38-
}()
30+
class ExampleViewController: UITableViewController {
3931

4032
private let types: [UIView.Type] = [
4133
ICKeyboardDismissTextField.self,
@@ -46,30 +38,28 @@ class ExampleViewController: UIViewController, UITableViewDataSource {
4638
// MARK: - Initialization
4739

4840
convenience init() {
49-
self.init(nibName: nil, bundle: nil)
41+
self.init(style: .Grouped)
5042
title = "ICInputAccessory"
5143
}
5244

5345
// MARK: - UIViewController
5446

5547
override func loadView() {
5648
super.loadView()
57-
view.addSubview(tableView)
58-
tableView.frame = view.bounds
59-
tableView.autoresizingMask = [.FlexibleWidth, .FlexibleHeight]
49+
tableView.registerClass(ExampleCell.self, forCellReuseIdentifier: NSStringFromClass(ExampleCell.self))
6050
}
6151

6252
// MARK: - UITableViewDataSource
6353

64-
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
54+
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
6555
return types.count
6656
}
6757

68-
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
58+
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
6959
return 1
7060
}
7161

72-
func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
62+
override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
7363
switch types[section] {
7464
case is ICKeyboardDismissTextField.Type:
7565
return "Dismiss Keyboard"
@@ -82,7 +72,7 @@ class ExampleViewController: UIViewController, UITableViewDataSource {
8272
}
8373
}
8474

85-
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
75+
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
8676
let cell = tableView.dequeueReusableCellWithIdentifier(NSStringFromClass(ExampleCell.self), forIndexPath: indexPath)
8777
switch types[indexPath.section] {
8878
case let type as ICKeyboardDismissTextField.Type:
@@ -92,6 +82,10 @@ class ExampleViewController: UIViewController, UITableViewDataSource {
9282
textField.placeholder = String(type)
9383
(cell as? ExampleCell)?.showcase = textField
9484

85+
case let type as CustomizedTokenField.Type:
86+
cell.textLabel?.text = String(type)
87+
cell.accessoryType = .DisclosureIndicator
88+
9589
case let type as ICTokenField.Type:
9690
let container = UIView(frame: cell.bounds)
9791
let tokenField = type.init()
@@ -101,13 +95,22 @@ class ExampleViewController: UIViewController, UITableViewDataSource {
10195
container.addSubview(tokenField)
10296
(cell as? ExampleCell)?.showcase = container
10397

104-
case let type as CustomizedTokenField.Type:
105-
(cell as? ExampleCell)?.showcase = type.init()
106-
10798
default:
10899
break
109100
}
110101
return cell
111102
}
112103

104+
// MARK: - UITableViewDelegate
105+
106+
override func tableView(tableView: UITableView, shouldHighlightRowAtIndexPath indexPath: NSIndexPath) -> Bool {
107+
return types[indexPath.section] == CustomizedTokenField.self
108+
}
109+
110+
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
111+
if types[indexPath.section] == CustomizedTokenField.self {
112+
presentViewController(UINavigationController(rootViewController: CustomizedTokenViewController()), animated: true, completion: nil)
113+
}
114+
}
115+
113116
}

0 commit comments

Comments
 (0)