-
Notifications
You must be signed in to change notification settings - Fork 651
Expand file tree
/
Copy pathViewController.swift
More file actions
312 lines (252 loc) · 8.87 KB
/
ViewController.swift
File metadata and controls
312 lines (252 loc) · 8.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
//
// ViewController.swift
// DropDown
//
// Created by Kevin Hirsch on 28/07/15.
// Copyright (c) 2015 Kevin Hirsch. All rights reserved.
//
import UIKit
import DropDown
class ViewController: UIViewController {
//MARK: - Properties
@IBOutlet weak var chooseArticleButton: UIButton!
@IBOutlet weak var amountButton: UIButton!
@IBOutlet weak var chooseButton: UIButton!
@IBOutlet weak var centeredDropDownButton: UIButton!
@IBOutlet weak var rightBarButton: UIBarButtonItem!
let textField = UITextField()
//MARK: - DropDown's
let chooseArticleDropDown = DropDown()
let amountDropDown = DropDown()
let chooseDropDown = DropDown()
let centeredDropDown = DropDown()
let rightBarDropDown = DropDown()
lazy var dropDowns: [DropDown] = {
return [
self.chooseArticleDropDown,
self.amountDropDown,
self.chooseDropDown,
self.centeredDropDown,
self.rightBarDropDown
]
}()
//MARK: - Actions
@IBAction func chooseArticle(_ sender: AnyObject) {
chooseArticleDropDown.show()
}
@IBAction func changeAmount(_ sender: AnyObject) {
amountDropDown.show()
}
@IBAction func choose(_ sender: AnyObject) {
chooseDropDown.show()
}
@IBAction func showCenteredDropDown(_ sender: AnyObject) {
centeredDropDown.show()
}
@IBAction func showBarButtonDropDown(_ sender: AnyObject) {
rightBarDropDown.show()
}
@IBAction func changeDIsmissMode(_ sender: UISegmentedControl) {
switch sender.selectedSegmentIndex {
case 0: dropDowns.forEach { $0.dismissMode = .automatic }
case 1: dropDowns.forEach { $0.dismissMode = .onTap }
default: break;
}
}
@IBAction func changeDirection(_ sender: UISegmentedControl) {
switch sender.selectedSegmentIndex {
case 0: dropDowns.forEach { $0.direction = .any }
case 1: dropDowns.forEach { $0.direction = .bottom }
case 2: dropDowns.forEach { $0.direction = .top }
default: break;
}
}
@IBAction func changeUI(_ sender: UISegmentedControl) {
switch sender.selectedSegmentIndex {
case 0: setupDefaultDropDown()
case 1: customizeDropDown(self)
case 2: customizeDropDownWithClass(self)
case 3: customizeDropDownSelector(self)
default: break;
}
}
@IBAction func showKeyboard(_ sender: AnyObject) {
textField.becomeFirstResponder()
}
@IBAction func hideKeyboard(_ sender: AnyObject) {
view.endEditing(false)
}
func setupDefaultDropDown() {
DropDown.setupDefaultAppearance()
dropDowns.forEach {
$0.cellNib = UINib(nibName: "DropDownCell", bundle: Bundle(for: DropDownCell.self))
$0.customCellConfiguration = nil
}
}
func customizeDropDown(_ sender: AnyObject) {
let appearance = DropDown.appearance()
appearance.cellHeight = 60
appearance.backgroundColor = UIColor(white: 1, alpha: 1)
appearance.selectionBackgroundColor = UIColor(red: 0.6494, green: 0.8155, blue: 1.0, alpha: 0.2)
// appearance.separatorColor = UIColor(white: 0.7, alpha: 0.8)
appearance.cornerRadius = 10
appearance.shadowColor = UIColor(white: 0.6, alpha: 1)
appearance.shadowOpacity = 0.9
appearance.shadowRadius = 25
appearance.animationduration = 0.25
appearance.textColor = .darkGray
// appearance.textFont = UIFont(name: "Georgia", size: 14)
if #available(iOS 11.0, *) {
appearance.setupMaskedCorners([.layerMaxXMaxYCorner, .layerMinXMaxYCorner])
}
dropDowns.forEach {
/*** FOR CUSTOM CELLS ***/
$0.cellNib = UINib(nibName: "MyCell", bundle: nil)
$0.customCellConfiguration = { (index: Index, item: String, cell: DropDownCell) -> Void in
guard let cell = cell as? MyCell else { return }
// Setup your custom UI components
cell.logoImageView.image = UIImage(named: "logo_\(index % 10)")
}
/*** ---------------- ***/
}
}
func customizeDropDownWithClass(_ sender: AnyObject) {
dropDowns.forEach {
/*** FOR CUSTOM CELLS ***/
$0.cellClass = MySecondCell.self
$0.customCellConfiguration = { (index: Index, item: String, cell: DropDownCell) -> Void in
guard let cell = cell as? MySecondCell else { return }
// Setup your custom UI components
cell.logoImageView.image = UIImage(named: "logo_\(index % 10)")
}
/*** ---------------- ***/
}
}
func customizeDropDownSelector(_ sender: AnyObject) {
dropDowns.forEach {
/*** FOR CUSTOM CELLS ***/
$0.cellClass = CheckboxCell.self
$0.customCellConfiguration = nil
}
}
//MARK: - UIViewController
override func viewDidLoad() {
super.viewDidLoad()
setupDropDowns()
dropDowns.forEach { $0.dismissMode = .onTap }
dropDowns.forEach { $0.direction = .any }
dropDowns.forEach { $0.showArrowIndicator = true }
view.addSubview(textField)
}
//MARK: - Setup
func setupDropDowns() {
setupChooseArticleDropDown()
setupAmountDropDown()
setupChooseDropDown()
setupCenteredDropDown()
setupRightBarDropDown()
}
func setupChooseArticleDropDown() {
chooseArticleDropDown.anchorView = chooseArticleButton
// Will set a custom with instead of anchor view width
// dropDown.width = 100
// By default, the dropdown will have its origin on the top left corner of its anchor view
// So it will come over the anchor view and hide it completely
// If you want to have the dropdown underneath your anchor view, you can do this:
chooseArticleDropDown.bottomOffset = CGPoint(x: 0, y: chooseArticleButton.bounds.height)
// You can also use localizationKeysDataSource instead. Check the docs.
chooseArticleDropDown.dataSource = [
"iPhone SE | Black | 64G",
"Samsung S7",
"Huawei P8 Lite Smartphone 4G",
"Asus Zenfone Max 4G",
"Apple Watwh | Sport Edition"
]
// Action triggered on selection
chooseArticleDropDown.selectionAction = { [weak self] (index, item) in
self?.chooseArticleButton.setTitle(item, for: .normal)
}
chooseArticleDropDown.multiSelectionAction = { [weak self] (indices, items) in
print("Muti selection action called with: \(items)")
if items.isEmpty {
self?.chooseArticleButton.setTitle("", for: .normal)
}
}
// Action triggered on dropdown cancelation (hide)
// dropDown.cancelAction = { [unowned self] in
// // You could for example deselect the selected item
// self.dropDown.deselectRowAtIndexPath(self.dropDown.indexForSelectedRow)
// self.actionButton.setTitle("Canceled", forState: .Normal)
// }
// You can manually select a row if needed
// dropDown.selectRowAtIndex(3)
}
func setupAmountDropDown() {
amountDropDown.anchorView = amountButton
// By default, the dropdown will have its origin on the top left corner of its anchor view
// So it will come over the anchor view and hide it completely
// If you want to have the dropdown underneath your anchor view, you can do this:
amountDropDown.bottomOffset = CGPoint(x: 0, y: amountButton.bounds.height)
// You can also use localizationKeysDataSource instead. Check the docs.
amountDropDown.dataSource = [
"10 €",
"20 €",
"30 €",
"40 €",
"50 €",
"60 €",
"70 €",
"80 €",
"90 €",
"100 €",
"110 €",
"120 €"
]
// Action triggered on selection
amountDropDown.selectionAction = { [weak self] (index, item) in
self?.amountButton.setTitle(item, for: .normal)
}
}
func setupChooseDropDown() {
chooseDropDown.anchorView = chooseButton
// By default, the dropdown will have its origin on the top left corner of its anchor view
// So it will come over the anchor view and hide it completely
// If you want to have the dropdown underneath your anchor view, you can do this:
chooseDropDown.bottomOffset = CGPoint(x: 0, y: chooseButton.bounds.height)
// You can also use localizationKeysDataSource instead. Check the docs.
chooseDropDown.dataSource = [
"Lorem ipsum dolor",
"sit amet consectetur",
"cadipisci en..."
]
// Action triggered on selection
chooseDropDown.selectionAction = { [weak self] (index, item) in
self?.chooseButton.setTitle(item, for: .normal)
}
}
func setupCenteredDropDown() {
// Not setting the anchor view makes the drop down centered on screen
// centeredDropDown.anchorView = centeredDropDownButton
// You can also use localizationKeysDataSource instead. Check the docs.
centeredDropDown.dataSource = [
"The drop down",
"Is centered on",
"the view because",
"it has no anchor view defined.",
"Click anywhere to dismiss."
]
centeredDropDown.selectionAction = { [weak self] (index, item) in
self?.centeredDropDownButton.setTitle(item, for: .normal)
}
}
func setupRightBarDropDown() {
rightBarDropDown.anchorView = rightBarButton
// You can also use localizationKeysDataSource instead. Check the docs.
rightBarDropDown.dataSource = [
"Menu 1",
"Menu 2",
"Menu 3",
"Menu 4"
]
}
}