Skip to content

Commit 11022d9

Browse files
committed
#6 Observe changes in contentSize and layout of ScrollStack via delegate
1 parent 3b30d8d commit 11022d9

4 files changed

Lines changed: 63 additions & 3 deletions

File tree

ScrollStackControllerDemo/ViewController.swift

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88

99
import UIKit
1010

11-
class ViewController: UIViewController {
11+
class ViewController: UIViewController, ScrollStackControllerDelegate {
12+
1213

1314
@IBOutlet public var contentView: UIView!
1415

@@ -28,13 +29,19 @@ class ViewController: UIViewController {
2829
override func viewDidLoad() {
2930
super.viewDidLoad()
3031
}
32+
33+
func scrollStackRowDidUpdateLayout(_ stackView: ScrollStack) {
34+
debugPrint("New content insets \(stackView.contentSize.height)")
35+
}
3136

3237
override func viewDidAppear(_ animated: Bool) {
3338
super.viewDidAppear(animated)
3439

3540
stackController.view.frame = contentView.bounds
3641
contentView.addSubview(stackController.view)
3742

43+
44+
stackView.stackDelegate = self
3845
// Prepare content
3946

4047
welcomeVC = WelcomeVC.create()
@@ -44,7 +51,6 @@ class ViewController: UIViewController {
4451
notesVC = NotesVC.create(delegate: self)
4552

4653
stackView.addRows(controllers: [welcomeVC, notesVC, tagsVC, galleryVC,pricingVC], animated: false)
47-
4854
}
4955

5056
@IBAction public func addNewRow() {
@@ -78,6 +84,28 @@ class ViewController: UIViewController {
7884
let randomRow = Int.random(in: 0..<stackView.rows.count)
7985
stackView.scrollToRow(index: randomRow, at: .middle, animated: true)
8086
}
87+
88+
89+
func scrollStackDidScroll(_ stackView: ScrollStack, offset: CGPoint) {
90+
91+
}
92+
93+
func scrollStackRowDidBecomeVisible(_ stackView: ScrollStack, row: ScrollStackRow, index: Int, state: ScrollStack.RowVisibility) {
94+
95+
}
96+
97+
func scrollStackRowDidBecomeHidden(_ stackView: ScrollStack, row: ScrollStackRow, index: Int, state: ScrollStack.RowVisibility) {
98+
99+
}
100+
101+
func scrollStackDidUpdateLayout(_ stackView: ScrollStack) {
102+
103+
}
104+
105+
func scrollStackContentSizeDidChange(_ stackView: ScrollStack, from oldValue: CGSize, to newValue: CGSize) {
106+
// debugPrint("Content size did change from \(oldValue) to \(newValue)")
107+
}
108+
81109
}
82110

83111
extension ViewController: TagsVCProtocol {

Sources/ScrollStackController/ScrollStack.swift

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,10 @@ open class ScrollStack: UIScrollView, UIScrollViewDelegate {
4949
public static let defaultSeparatorColor = (UITableView().separatorColor ?? .clear)
5050
public static let defaultRowColor = UIColor.clear
5151
public static let defaultRowHighlightColor = UIColor(red: 0.85, green: 0.85, blue: 0.85, alpha: 1)
52-
52+
53+
/// Cached content size for did change content size callback in scrollstack delegate.
54+
private var cachedContentSize: CGSize?
55+
5356
// MARK: Public Properties
5457

5558
/// The direction that rows are laid out in the stack view and scrolling works.
@@ -953,4 +956,20 @@ open class ScrollStack: UIScrollView, UIScrollViewDelegate {
953956
dispatchRowsVisibilityChangesTo(stackDelegate)
954957
}
955958

959+
open override func layoutSubviews() {
960+
super.layoutSubviews()
961+
962+
guard let stackDelegate = stackDelegate else {
963+
return
964+
}
965+
966+
stackDelegate.scrollStackDidUpdateLayout(self)
967+
968+
if let oldContentSize = cachedContentSize, oldContentSize != self.contentSize {
969+
stackDelegate.scrollStackContentSizeDidChange(self, from: oldContentSize, to: contentSize)
970+
}
971+
972+
cachedContentSize = self.contentSize
973+
}
974+
956975
}

Sources/ScrollStackController/Support/ScrollStack+Protocols.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,19 @@ public protocol ScrollStackControllerDelegate: class {
8282
/// - Parameter state: state of the row.
8383
func scrollStackRowDidBecomeHidden(_ stackView: ScrollStack, row: ScrollStackRow, index: Int, state: ScrollStack.RowVisibility)
8484

85+
86+
/// This function is called when layout is updated (added, removed, hide or show one or more rows).
87+
/// - Parameter stackView: target stack view.
88+
func scrollStackDidUpdateLayout(_ stackView: ScrollStack)
89+
90+
/// This function is called when content size of the stack did change (remove/add, hide/show rows).
91+
///
92+
/// - Parameters:
93+
/// - stackView: target stack view
94+
/// - oldValue: old content size.
95+
/// - newValue: new content size.
96+
func scrollStackContentSizeDidChange(_ stackView: ScrollStack, from oldValue: CGSize, to newValue: CGSize)
97+
8598
}
8699

87100
// MARK: - ScrollStackRowHighlightable

0 commit comments

Comments
 (0)