Skip to content

Commit b9591b0

Browse files
committed
Still working on doc, adding rows
1 parent 6ef1280 commit b9591b0

File tree

3 files changed

+69
-7
lines changed

3 files changed

+69
-7
lines changed

README.md

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ You can think of it as `UITableView` but with several differences:
1515
- Main Features
1616
- System Requirements
1717
- How to use it
18+
- Adding Rows
19+
- Removing Rows
1820

1921
### Main Features
2022

@@ -36,3 +38,62 @@ You can think of it as `UITableView` but with several differences:
3638
- Swift 5+
3739

3840
### How to use it
41+
42+
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.
43+
44+
However usually you don't want to intantiate this control directly but by calling the `ScrollStackController` class.
45+
It's a view controller which allows you to get the child view controller's managment for free, so when you add/remove a row to the stack you will get the standard UIViewController events for free!
46+
47+
This is an example of initialization in a view controller:
48+
49+
```swift
50+
class MyViewController: UIViewController {
51+
52+
private var stackController = ScrollStackViewController()
53+
54+
override func viewDidLoad() {
55+
super.viewDidLoad()
56+
contentView.addSubview(stackController.view)
57+
}
58+
59+
}
60+
```
61+
62+
Now you are ready to use the `ScrollStack` control inside the `stackController` class.
63+
`ScrollStack` have an extensible rich set of APIs to manage your layout: add, remove, move, hide or show your rows, including insets and separator management.
64+
65+
Each row managed by `ScrollStack` is a subclass of `ScrollStackRow`: it strongly reference a parent `UIViewController` class where you content is placed. `UIViewController`'s `view` will be the `contentView` of the row.
66+
67+
You don't need to handle lifecycle of your rows/view controller until they are part of the rows inside the stack.
68+
69+
Let's take a look below.
70+
71+
#### Adding Rows
72+
73+
`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.
74+
75+
To add row you can use one the following methods:
76+
77+
- `addRow(controller:at:animated:) -> ScrollStackRow?`
78+
- `addRows(controllers:at:animated:) -> [ScrollStackRow]?`
79+
80+
Both of these methods takes as arguments:
81+
82+
- `controller/s`: one or more `UIViewController` instances; each view of these controllers will be as a row of the stack inside a `ScrollStackRow` (a sort of cell).
83+
- `at`: specify the insertion point. It's an enum with the following options: `top` (at first index), `bottom` (append at the bottom of the list), `atIndex` (specific index), `after` or `below` (after/below a row which contain a specific `UIViewController`).
84+
- `animated`: if true insertion will be animated
85+
- `completion`: completion callback to call at the end of the operation.
86+
87+
The following code add a rows with the view of each view controller passed:
88+
89+
```swift
90+
let welcomeVC = WelcomeVC.create()
91+
let tagsVC = TagsVC.create(delegate: self)
92+
let galleryVC = GalleryVC.create()
93+
94+
stackView.addRows(controllers: [welcomeVC, notesVC, tagsVC, galleryVC], animated: false)
95+
```
96+
97+
As you noted 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.
98+
99+
#### Removing Rows

Sources/ScrollStackController/ScrollStack.swift

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -207,37 +207,38 @@ open class ScrollStack: UIScrollView, UIScrollViewDelegate {
207207
}
208208

209209
// MARK: - Insert Rows
210-
211210
/// Insert a new row to manage passed controller instance.
212211
///
213212
/// - Parameter controller: controller to manage; it's `view` will be added as contentView of the row.
214213
/// - Parameter location: location inside the stack of the new row.
215214
/// - Parameter animated: `true` to animate operation, by default is `false`.
215+
/// - Parameter completion: optional completion callback to call at the end of insertion.
216216
@discardableResult
217-
open func addRow(controller: UIViewController, at location: InsertLocation = .bottom, animated: Bool = false) -> ScrollStackRow? {
217+
open func addRow(controller: UIViewController, at location: InsertLocation = .bottom, animated: Bool = false, completion: (() -> Void)? = nil) -> ScrollStackRow? {
218218
switch location {
219219
case .top:
220-
return createRowForController(controller, insertAt: 0, animated: animated)
220+
return createRowForController(controller, insertAt: 0, animated: animated, completion: completion)
221221

222222
case .bottom:
223-
return createRowForController(controller, insertAt: rows.count, animated: animated)
223+
return createRowForController(controller, insertAt: rows.count, animated: animated, completion: completion)
224224

225225
case .atIndex(let index):
226-
return createRowForController(controller, insertAt: index, animated: animated)
226+
return createRowForController(controller, insertAt: index, animated: animated, completion: completion)
227227

228228
case .after(let afterController):
229229
guard let index = rowForController(afterController)?.index else {
230230
return nil
231231
}
232232

233-
return createRowForController(controller, insertAt: ((index + 1) >= rows.count ? rows.count : (index + 1)), animated: animated)
233+
let finalIndex = ((index + 1) >= rows.count ? rows.count : (index + 1))
234+
return createRowForController(controller, insertAt: finalIndex, animated: animated, completion: completion)
234235

235236
case .before(let beforeController):
236237
guard let index = rowForController(beforeController)?.index else {
237238
return nil
238239
}
239240

240-
return createRowForController(controller, insertAt: index, animated: animated)
241+
return createRowForController(controller, insertAt: index, animated: animated, completion: completion)
241242

242243
}
243244
}

0 commit comments

Comments
 (0)