You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
ScrollStackController was created and maintaned by [Daniele Margutti](https://github.com/malcommac) - My home site [www.danielemargutti.com](https://www.danielemargutti.com).
11
+
4
12
## Introduction
5
13
ScrollStackController is a class you can use to create complex layouts using scrollable `UIStackView` but where each row is handled by a separate `UIViewController`; this allows you to keep a great separation of concerns.
6
14
@@ -12,16 +20,26 @@ You can think of it as `UITableView` but with several differences:
12
20
13
21
## Table of Contents
14
22
15
-
- Main Features
16
-
- System Requirements
17
-
- How to use it
18
-
- Adding Rows
19
-
- Removing / Replacing Rows
20
-
- Move Rows
21
-
- Hide / Show Rows
22
-
- Reload Rows
23
-
- Sizing Rows
24
-
23
+
-[Main Features](#mainfeatures)
24
+
-[System Requirements](#systemrequirements)
25
+
-[When to use `ScrollStackController` and when not](#whentousescrollstackcontrollerandwhennot)
### When to use `ScrollStackController` and when not
65
+
66
+
##### Yes
67
+
68
+
`ScrollStackController` is best used for shorter screens with an heterogeneous set of rows: in these cases you don't need to have view recycling.
69
+
70
+
Thanks to autolayout you will get updates and animations for free.
71
+
72
+
You can also manage each screen independently with a great separation of concerns; morehover unlike `UITableView` and `UICollectionView`, you can keep strong references to `UIViewController` (and its views) in an `ScrollStack` view and make changes to them at any point.
73
+
74
+
#### No
75
+
76
+
`ScrollStackController` is not suitable in all situations.
77
+
`ScrollStackController` lays out the entire UI at first time when your screen loads.
78
+
If you have a long list of rows you may experience delays.
79
+
80
+
So, `ScrollStackController` is generally not appropriate for screens that contain many views of the same type, all showing similar data (in these cases you should use `UITableView` or `UICollectionView`).
81
+
82
+
<aname="howtouseit"/>
44
83
### How to use it
45
84
46
85
The main class of the package is `ScrollStack`, a subclass of `UIScrollView`. It manages the layout of each row, animations and keep a strong reference to your rows.
@@ -89,6 +128,7 @@ let lastRow = scrollStack.lastRow
89
128
90
129
Let's take a look below.
91
130
131
+
<aname="addingrows"/>
92
132
#### Adding Rows
93
133
94
134
`ScrollStack` provides a comprehensive set of methods for managing rows, including inserting rows at the beginning and end, inserting rows above or below other rows.
@@ -117,6 +157,7 @@ The following code add a rows with the view of each view controller passed:
117
157
118
158
As you noticed there is not need to keep a strong reference to any view controller; they are automatically strong referenced by each row created to add them into the stack.
119
159
160
+
<aname="removingreplacingrows"/>
120
161
#### Removing / Replacing Rows
121
162
122
163
A similar set of APIs are used to remove existing rows from the stack:
Keep in mind: when you hide a rows the row still part of the stack and it's not removed, just hidden! If you get the list of rows by calling `rows` property of the `ScrollStack` you still see it.
165
208
209
+
<aname="reloadrows"/>
166
210
#### Reload Rows
167
211
168
212
Reload rows method allows you to refresh the layout of the entire stack (using `layoutIfNeeded()`) while you have a chance to update a specific row's `contentView` (aka the view of the managed `UIViewController`).
@@ -199,6 +243,7 @@ class GalleryVC: UIViewController, ScrollStackContainableController {
199
243
}
200
244
```
201
245
246
+
<aname="sizingrows"/>
202
247
#### Sizing Rows
203
248
204
249
You can control the size of your `UIViewController` inside a row of a `ScrollStack` in two ways:
@@ -208,3 +253,200 @@ You can control the size of your `UIViewController` inside a row of a `ScrollSta
208
253
209
254
In both case `ScrollStack` class will use only one dimension depending by the active scroll axis to layout the view controller content into the stack (if scroll axis is `horizontal` you can control only the `height` of the row, if it's `vertical` only the `width`. The other dimension will be the same of the scroll stack itself.
210
255
256
+
Each of the following cases is covered inside the demo application:
257
+
258
+
- Fixed row size in [GalleryVC](https://github.com/malcommac/ScrollStackController/blob/master/ScrollStackControllerDemo/Child%20View%20Controllers/GalleryVC.swift)
259
+
- Collapsible / Expandable row in [TagsVC](https://github.com/malcommac/ScrollStackController/blob/master/ScrollStackControllerDemo/Child%20View%20Controllers/TagsVC.swift)
260
+
- Growing row based on `UITextView`'s content in [NotesVC](https://github.com/malcommac/ScrollStackController/blob/master/ScrollStackControllerDemo/Child%20View%20Controllers/NotesVC.swift)
261
+
- Growing row based on `UITableView`'s content in [PricingVC](https://github.com/malcommac/ScrollStackController/blob/master/ScrollStackControllerDemo/Child%20View%20Controllers/PricingVC.swift)
262
+
263
+
<aname="fixedrowsize"/>
264
+
#### Fixed Row Size
265
+
266
+
If your view controller has a fixed size you can just return it as follows:
If your stack support single axis you can obivously avoid switch condition.
286
+
When you will add this view controller in a scroll stack it will be sized as you requested (any height/width constraint already in place will be removed).
287
+
288
+
<aname="fittinglayoutrowsize"/>
289
+
#### Fitting Layout Row Size
290
+
291
+
Sometimes you may want to have the content view sized by fitting the contents of the view controller's view. In these cases you can use `. fitLayoutForAxis`.
pricingTableHeightConstraint.constant= pricingTable.contentSize.height// the size of the table as the size of its content
370
+
view.height(constant: nil) // cancel any height constraint already in place in the view
371
+
super.updateViewConstraints()
372
+
}
373
+
}
374
+
```
375
+
376
+
In this way as you add new value to the table the size of the row in stack view will grown.
377
+
378
+
<aname="rowsseparator"/>
379
+
#### Rows Separator
380
+
381
+
Each row managed by `ScrollStack` is of a subview class of type `ScrollStackRow`. It has a strong referenced to managed `UIViewController` but also have a subview on bottom called `ScrollStackSeparator`.
382
+
383
+
You can hide/show separators by using the following properties of the row:
384
+
385
+
-`isSeparatorHidden`: to hide separator.
386
+
-`separatorInsets`: to set the insets of the sepatator (by default is set to the same value used by `UITableView` instances)
387
+
-`separatorView.color`: to change the color
388
+
-`separatorView.thickness`: to se the thickness of the separator (1 by default).
389
+
390
+
Moreover you can set these values directly on `ScrollStack` controller in order to have a default value for each new row.
391
+
392
+
`ScrollStack` also have a property called `autoHideLastRowSeparator` to hide the last separator of the stack automatically.
393
+
394
+
<aname="taponrows"/>
395
+
#### Tap On Rows
396
+
397
+
By default rows are not tappable but if you need to implement some sort of tap features like in `UITableView` you can add it by setting a default callback for `onTap` property on `ScrollStackRow` instances.
398
+
399
+
For example:
400
+
401
+
```swift
402
+
scrollStack.firstRow?.onTap= { row in
403
+
// do something on tap
404
+
}
405
+
```
406
+
407
+
Once you can set a tap handler you can also provide highlight color for tap.
408
+
To do it you must implement `ScrollStackRowHighlightable` protocol in your row managed view controller.
0 commit comments