-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathStackView.swift
More file actions
439 lines (374 loc) · 14.3 KB
/
StackView.swift
File metadata and controls
439 lines (374 loc) · 14.3 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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
// Copyright (c) 2017 Luc Dion
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
import Foundation
#if os(iOS) || os(tvOS)
import UIKit
public class StackView: UIView {
internal var direction = SDirection.column
internal var justifyContent = SJustifyContent.start
internal var alignItems = SAlignItems.stretch
internal var _paddingTop: Value?
internal var _paddingLeft: Value?
internal var _paddingStart: Value?
internal var _paddingBottom: Value?
internal var _paddingRight: Value?
internal var _paddingEnd: Value?
public override init(frame: CGRect) {
super.init(frame: frame)
translatesAutoresizingMaskIntoConstraints = false
}
public required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
translatesAutoresizingMaskIntoConstraints = false
}
public static override var requiresConstraintBasedLayout: Bool {
return false
}
/**
This method is used to structure your code so that it matches the stack view structure. The method has a closure parameter with a
single parameter called `stackView`. This parameter is in fact the StackView instance, it can be used to adds items
to the StackView.
- Parameter closure:
*/
@discardableResult
public func define(_ closure: (_ stackView: StackView) -> Void) -> StackView {
closure(self)
return self
}
@discardableResult
public func addStackView() -> StackView {
let stackView = StackView()
addItem(stackView)
return stackView
}
@discardableResult
public func addItem(_ view: UIView) -> StackItem {
view.translatesAutoresizingMaskIntoConstraints = false
addSubview(view)
markDirty()
return view.item
}
@discardableResult
public func insertItem(_ view: UIView, at index: Int) -> StackItem {
view.translatesAutoresizingMaskIntoConstraints = false
insertSubview(view, at: index)
markDirty()
return view.item
}
@discardableResult
public func insertItem(_ view: UIView, before refItem: UIView) -> StackItem? {
view.translatesAutoresizingMaskIntoConstraints = false
insertSubview(view, aboveSubview: refItem)
markDirty()
return view.item
}
@discardableResult
public func insertItem(_ view: UIView, after refItem: UIView) -> StackItem? {
view.translatesAutoresizingMaskIntoConstraints = false
insertSubview(view, belowSubview: refItem)
markDirty()
return view.item
}
public func removeItem(_ view: UIView) {
view.removeFromSuperview()
markDirty()
}
/**
The `direction` property establishes the main-axis, thus defining the direction items are placed in the StackView.
The `direction` property specifies how items are laid out in the StackView, by setting the direction of the StackView’s main axis. They can be laid out in two main directions, like columns vertically or like rows horizontally.
- Parameter value: Default value is .column
*/
@discardableResult
public func direction(_ value: SDirection) -> StackView {
direction = value
markDirty()
return self
}
public func getDirection() -> SDirection {
return direction
}
/**
The `justifyContent` property defines the alignment along the main-axis of the StackView.
It helps distribute extra free space leftover when either all the items on a line have reached their maximum
size. For example, if items are flowing vertically, `justifyContent` controls how they align vertically.
- Parameter value: Default value is .start
*/
@discardableResult
public func justifyContent(_ value: SJustifyContent) -> StackView {
justifyContent = value
markDirty()
return self
}
public func getJustifyContent() -> SJustifyContent {
return justifyContent
}
/**
The `alignItems` property defines how items are laid out along the cross-axis in the StackView.
Similar to `justifyContent` but for the cross-axis (perpendicular to the main-axis). For example, if
items are flowing vertically, `alignItems` controls how they align horizontally.
- Parameter value: Default value is .stretch
*/
@discardableResult
public func alignItems(_ value: SAlignItems) -> StackView {
alignItems = value
markDirty()
return self
}
public func getAlignItems() -> SAlignItems {
return alignItems
}
//
// MARK: Padding
//
/**
Set the top padding. Top padding specify the **offset children should have** from the container's top edge.
*/
@discardableResult
public func paddingTop(_ value: CGFloat) -> StackView {
_paddingTop = Value(value)
return self
}
/**
Set the left padding. Left padding specify the **offset children should have** from the container's left edge.
*/
@discardableResult
public func paddingLeft(_ value: CGFloat) -> StackView {
_paddingLeft = Value(value)
return self
}
/**
Set the bottom padding. Bottom padding specify the **offset children should have** from the container's bottom edge.
*/
@discardableResult
public func paddingBottom(_ value: CGFloat) -> StackView {
_paddingBottom = Value(value)
return self
}
/**
Set the top padding. Top padding specify the **offset children should have** from the container's top edge.
*/
@discardableResult
public func paddingRight(_ value: CGFloat) -> StackView {
_paddingRight = Value(value)
return self
}
/**
Set the start padding.
Depends on the item `LayoutDirection`:
* In LTR direction, start padding specify the **offset children should have** from the container's left edge.
* IN RTL direction, start padding specify the **offset children should have** from the container's right edge.
*/
@discardableResult
public func paddingStart(_ value: CGFloat) -> StackView {
_paddingStart = Value(value)
return self
}
/**
Set the end padding.
Depends on the item `LayoutDirection`:
* In LTR direction, end padding specify the **offset children should have** from the container's right edge.
* IN RTL direction, end padding specify the **offset children should have** from the container's left edge.
*/
@discardableResult
public func paddingEnd(_ value: CGFloat) -> StackView {
_paddingEnd = Value(value)
return self
}
/**
Set the left, right, start and end paddings to the specified value.
*/
@discardableResult
public func paddingHorizontal(_ value: CGFloat) -> StackView {
_paddingLeft = Value(value)
_paddingRight = Value(value)
return self
}
/**
Set the top and bottom paddings to the specified value.
*/
@discardableResult
public func paddingVertical(_ value: CGFloat) -> StackView {
_paddingTop = Value(value)
_paddingBottom = Value(value)
return self
}
/**
Set paddings using UIEdgeInsets.
This method is particularly useful to set all paddings using iOS 11 `UIView.safeAreaInsets`.
*/
@discardableResult
public func padding(insets: UIEdgeInsets) -> StackView {
_paddingTop = Value(insets.top)
_paddingLeft = Value(insets.left)
_paddingBottom = Value(insets.bottom)
_paddingRight = Value(insets.right)
return self
}
/**
Set paddings using NSDirectionalEdgeInsets.
This method is particularly to set all paddings using iOS 11 `UIView.directionalLayoutMargins`.
Available only on iOS 11 and higher.
*/
@available(tvOS 11.0, iOS 11.0, *)
@discardableResult
func padding(dirInsets: NSDirectionalEdgeInsets) -> StackView {
_paddingTop = Value(dirInsets.top)
_paddingStart = Value(dirInsets.leading)
_paddingBottom = Value(dirInsets.bottom)
_paddingEnd = Value(dirInsets.trailing)
return self
}
/**
Set all paddings to the specified value.
*/
@discardableResult
public func padding(_ value: CGFloat) -> StackView {
_paddingTop = Value(value)
_paddingBottom = _paddingTop
_paddingLeft = _paddingTop
_paddingRight = _paddingTop
return self
}
/**
Set the individually vertical paddings (top, bottom) and horizontal paddings (left, right, start, end).
*/
@discardableResult func padding(_ vertical: CGFloat, _ horizontal: CGFloat) -> StackView {
paddingVertical(vertical)
paddingHorizontal(horizontal)
return self
}
/**
Set the individually top, horizontal paddings and bottom padding.
*/
@discardableResult func padding(_ top: CGFloat, _ horizontal: CGFloat, _ bottom: CGFloat) -> StackView {
_paddingTop = Value(top)
paddingHorizontal(horizontal)
_paddingBottom = Value(bottom)
return self
}
/**
Set the individually top, left, bottom and right paddings.
*/
@discardableResult
public func padding(_ top: CGFloat, _ left: CGFloat, _ bottom: CGFloat, _ right: CGFloat) -> StackView {
_paddingTop = Value(top)
_paddingLeft = Value(left)
_paddingBottom = Value(bottom)
_paddingRight = Value(right)
return self
}
//
// Layout view
//
/**
StackView's items are layouted only when a item's property is changed and when the StackView size change.
In the event that you want to force StackView to do a layout of its items, you can mark it as dirty
using `markDirty()`.
*/
public func markDirty() {
setNeedsLayout()
// All StackViews ancestors dirty.
if let stackView = superview as? StackView {
stackView.markDirty()
}
}
//
// MARK: UIView Visual properties
//
/**
Set the item/view background color.
*/
@discardableResult
public func backgroundColor(_ color: UIColor) -> StackView {
backgroundColor = color
return self
}
/**
Set the item/view transparency `alpha`.
*/
@discardableResult
public func alpha(_ value: CGFloat) -> StackView {
alpha = value
return self
}
//
// Show/hide items
//
public func hideItem(_ view: UIView, animate: Bool) {
guard let stackItemImpl = view.item as? StackItemImpl else { return }
stackItemImpl.isHidden = true
// updateItemVisibility(view: view, isVisible: false, animate: animate)
}
public func showItem(_ view: UIView, animate: Bool) {
// updateItemVisibility(view: view, isVisible: true, animate: animate)
}
// internal func updateItemVisibility(view: UIView, isVisible: Bool, animate: Bool) {
// guard let stackItemImpl = view.item as? StackItemImpl else { return }
// guard let itemIndex = stackItems.index(of: stackItemImpl) else { print("The view is not part of this stackView!"); return }
// let duration = 0.3
//
// if animate {
// if isVisible {
// view.bounds.size = direction == .column ? CGSize(width: view.frame.width, height: 0) : CGSize(width: 0, height: view.frame.height)
// view.isHidden = false
//
// UIView.animate(withDuration: duration, delay: 0, options: [.beginFromCurrentState], animations: {
// self.forceLayoutNow()
// })
// } else {
// let itemSnapshot = view.snapshotView(afterScreenUpdates: true)!
// itemSnapshot.frame = view.frame
// insertSubview(itemSnapshot, at: itemIndex)
// view.isHidden = true
//
// UIView.animate(withDuration: duration, delay: 0, options: [.beginFromCurrentState], animations: {
// itemSnapshot.frame.size = self.direction == .column ? CGSize(width: itemSnapshot.frame.width, height: 0) : CGSize(width: 0, height: itemSnapshot.frame.height)
// self.forceLayoutNow()
// }, completion: { (completed) in
// itemSnapshot.removeFromSuperview()
// })
// }
// } else {
// view.isHidden = !isVisible
// forceLayoutNow()
// }
// }
// internal func forceLayoutNow() {
// setNeedsLayout()
// layoutIfNeeded()
// }
// TODO_: Tests StackView using autolayout
public override var intrinsicContentSize: CGSize {
return sizeThatFits(CGSize(width: frame.width, height: .greatestFiniteMagnitude))
}
// TODO_: Tests StackView using autolayout
public override func systemLayoutSizeFitting(_ targetSize: CGSize) -> CGSize {
return sizeThatFits(targetSize)
}
public override func layoutSubviews() {
super.layoutSubviews()
let container = Container(stackView: self)
layoutItems(container: container, mode: .layouting)
}
public override func sizeThatFits(_ size: CGSize) -> CGSize {
let container = Container(stackView: self, size: size)
return layoutItems(container: container, mode: .measuring)
}
}
#endif