3232
3333import UIKit
3434
35+ // MARK: - ScrollStackRow
36+
3537open class ScrollStackRow : UIView , UIGestureRecognizerDelegate {
3638
3739 // MARK: Private Properties
@@ -52,12 +54,10 @@ open class ScrollStackRow: UIView, UIGestureRecognizerDelegate {
5254 } ( )
5355
5456 /// Constraints to handle separator's insets changes.
55- private var separatorConstraints : (
56- top: NSLayoutConstraint ,
57- bottom: NSLayoutConstraint ,
58- leading: NSLayoutConstraint ,
59- trailing: NSLayoutConstraint
60- ) ?
57+ private var separatorConstraints : ConstraintsHolder ?
58+
59+ /// Constraints to handle content's view padding changes.
60+ private var paddingConstraints : ConstraintsHolder ?
6161
6262 /// Location of the separator view.
6363 /// It's automatically managed when you change the axis of the parent stackview.
@@ -150,6 +150,13 @@ open class ScrollStackRow: UIView, UIGestureRecognizerDelegate {
150150 }
151151 }
152152
153+ open var rowPadding : UIEdgeInsets {
154+ didSet {
155+ paddingConstraints? . updateInsets ( rowPadding)
156+ layoutIfNeeded ( )
157+ }
158+ }
159+
153160 open override var isHidden : Bool {
154161 didSet {
155162 guard isHidden != oldValue else {
@@ -162,6 +169,7 @@ open class ScrollStackRow: UIView, UIGestureRecognizerDelegate {
162169 internal init ( controller: UIViewController , stackView: ScrollStack ) {
163170 self . stackView = stackView
164171 self . controller = controller
172+ self . rowPadding = stackView. rowPadding
165173 super. init ( frame: . zero)
166174
167175 clipsToBounds = true
@@ -198,6 +206,7 @@ open class ScrollStackRow: UIView, UIGestureRecognizerDelegate {
198206 }
199207
200208 rowInsets = stackView. rowInsets
209+ rowPadding = stackView. rowPadding
201210 rowBackgroundColor = stackView. rowBackgroundColor
202211 rowHighlightColor = stackView. rowHighlightColor
203212
@@ -212,40 +221,42 @@ open class ScrollStackRow: UIView, UIGestureRecognizerDelegate {
212221 // MARK: Manage Separator
213222
214223 private func didUpdateContentViewContraints( ) {
215- let bottomConstraint = contentView. bottomAnchor. constraint ( equalTo: layoutMarginsGuide. bottomAnchor)
224+ let bottomConstraint = contentView. bottomAnchor. constraint ( equalTo: layoutMarginsGuide. bottomAnchor, constant : rowPadding . bottom )
216225 bottomConstraint. priority = UILayoutPriority ( rawValue: UILayoutPriority . required. rawValue - 1 )
226+
227+ paddingConstraints = ConstraintsHolder (
228+ top: contentView. topAnchor. constraint ( equalTo: layoutMarginsGuide. topAnchor, constant: rowPadding. top) ,
229+ bottom: bottomConstraint,
230+ left: contentView. leadingAnchor. constraint ( equalTo: layoutMarginsGuide. leadingAnchor, constant: rowPadding. left) ,
231+ right: contentView. trailingAnchor. constraint ( equalTo: layoutMarginsGuide. trailingAnchor, constant: rowPadding. right)
232+ )
217233
218- NSLayoutConstraint . activate ( [
219- contentView. leadingAnchor. constraint ( equalTo: layoutMarginsGuide. leadingAnchor) ,
220- contentView. trailingAnchor. constraint ( equalTo: layoutMarginsGuide. trailingAnchor) ,
221- contentView. topAnchor. constraint ( equalTo: layoutMarginsGuide. topAnchor) ,
222- bottomConstraint
223- ] )
234+ paddingConstraints? . activateAll ( )
224235 }
225236
226237 private func didUpdateSeparatorViewContraintsIfNeeded( ) {
227238 if separatorConstraints == nil {
228- separatorConstraints = (
239+ separatorConstraints = ConstraintsHolder (
229240 top: separatorView. topAnchor. constraint ( equalTo: topAnchor) ,
230241 bottom: separatorView. bottomAnchor. constraint ( equalTo: bottomAnchor) ,
231- leading : separatorView. leadingAnchor. constraint ( equalTo: leadingAnchor) ,
232- trailing : separatorView. trailingAnchor. constraint ( equalTo: trailingAnchor)
242+ left : separatorView. leadingAnchor. constraint ( equalTo: leadingAnchor) ,
243+ right : separatorView. trailingAnchor. constraint ( equalTo: trailingAnchor)
233244 )
234245 }
235246 }
236247
237248 private func didUpdateSeparatorAxis( ) {
238- separatorConstraints? . top. isActive = ( separatorAxis == . vertical)
239- separatorConstraints? . bottom. isActive = true
240- separatorConstraints? . leading . isActive = ( separatorAxis == . horizontal)
241- separatorConstraints? . trailing . isActive = true
249+ separatorConstraints? . top? . isActive = ( separatorAxis == . vertical)
250+ separatorConstraints? . bottom? . isActive = true
251+ separatorConstraints? . left ? . isActive = ( separatorAxis == . horizontal)
252+ separatorConstraints? . right ? . isActive = true
242253 }
243254
244255 private func didUpdateSeparatorInsets( ) {
245- separatorConstraints? . top. constant = separatorInsets. top
246- separatorConstraints? . bottom. constant = ( separatorAxis == . horizontal ? 0 : - separatorInsets. bottom)
247- separatorConstraints? . leading . constant = separatorInsets. left
248- separatorConstraints? . trailing . constant = ( separatorAxis == . vertical ? 0 : - separatorInsets. right)
256+ separatorConstraints? . top? . constant = separatorInsets. top
257+ separatorConstraints? . bottom? . constant = ( separatorAxis == . horizontal ? 0 : - separatorInsets. bottom)
258+ separatorConstraints? . left ? . constant = separatorInsets. left
259+ separatorConstraints? . right ? . constant = ( separatorAxis == . vertical ? 0 : - separatorInsets. right)
249260 }
250261
251262 // MARK: - Sizing the Controller
@@ -370,3 +381,32 @@ open class ScrollStackRow: UIView, UIGestureRecognizerDelegate {
370381 }
371382
372383}
384+
385+ // MARK: - ConstraintsHolder
386+
387+ fileprivate class ConstraintsHolder {
388+ var top : NSLayoutConstraint ?
389+ var left : NSLayoutConstraint ?
390+ var bottom : NSLayoutConstraint ?
391+ var right : NSLayoutConstraint ?
392+
393+ init ( top: NSLayoutConstraint ? , bottom: NSLayoutConstraint ? ,
394+ left: NSLayoutConstraint ? , right: NSLayoutConstraint ? ) {
395+ self . top = top
396+ self . bottom = bottom
397+ self . left = left
398+ self . right = right
399+ }
400+
401+ func activateAll( ) {
402+ [ top, left, bottom, right] . forEach { $0? . isActive = true }
403+ }
404+
405+ func updateInsets( _ insets: UIEdgeInsets ) {
406+ top? . constant = insets. top
407+ bottom? . constant = insets. bottom
408+ left? . constant = insets. left
409+ right? . constant = insets. right
410+ }
411+
412+ }
0 commit comments