@@ -104,12 +104,12 @@ open class ScrollStackRow: UIView, UIGestureRecognizerDelegate {
104104 /// NOTE:
105105 /// This value is strongly retained so you don't need to
106106 /// save it anywhere in your parent controller in order to avoid releases.
107- public let controller : UIViewController ?
107+ public private ( set ) var controller : UIViewController ?
108108
109109 /// Content view controller (if controller is used) or just the view addded.
110110 ///
111111 /// NOTE: This value is strongly retained.
112- public let contentView : UIView
112+ public private ( set ) var contentView : UIView ?
113113
114114 // MARK: - Manage Separator
115115
@@ -138,7 +138,7 @@ open class ScrollStackRow: UIView, UIGestureRecognizerDelegate {
138138 // MARK: Private Properties
139139
140140 @objc private func handleTap( _ tapGestureRecognizer: UITapGestureRecognizer ) {
141- guard contentView. isUserInteractionEnabled else {
141+ guard contentView? . isUserInteractionEnabled ?? false else {
142142 return
143143 }
144144 onTap ? ( self )
@@ -196,8 +196,21 @@ open class ScrollStackRow: UIView, UIGestureRecognizerDelegate {
196196 }
197197
198198 // MARK: - Setup UI
199+
200+ public func removeFromStackView( ) {
201+ contentView? . removeFromSuperview ( )
202+ contentView = nil
203+ controller = nil
204+
205+ stackView? . stackView. removeArrangedSubview ( self )
206+ removeFromSuperview ( )
207+ }
199208
200209 private func setupPostInit( ) {
210+ guard let contentView = contentView else {
211+ return
212+ }
213+
201214 clipsToBounds = true
202215 insetsLayoutMarginsFromSafeArea = false
203216 contentView. translatesAutoresizingMaskIntoConstraints = false
@@ -207,6 +220,10 @@ open class ScrollStackRow: UIView, UIGestureRecognizerDelegate {
207220 }
208221
209222 internal func layoutUI( ) {
223+ guard let contentView = contentView else {
224+ return
225+ }
226+
210227 contentView. removeFromSuperview ( )
211228 contentView. removeFromSuperview ( )
212229
@@ -243,6 +260,10 @@ open class ScrollStackRow: UIView, UIGestureRecognizerDelegate {
243260 // MARK: - Manage Separator
244261
245262 private func didUpdateContentViewContraints( ) {
263+ guard let contentView = contentView else {
264+ return
265+ }
266+
246267 let bottomConstraint = contentView. bottomAnchor. constraint ( equalTo: layoutMarginsGuide. bottomAnchor, constant: rowPadding. bottom)
247268 bottomConstraint. priority = UILayoutPriority ( rawValue: UILayoutPriority . required. rawValue - 1 )
248269
@@ -305,7 +326,7 @@ open class ScrollStackRow: UIView, UIGestureRecognizerDelegate {
305326 }
306327
307328 private func setupRowToFixedValue( _ value: CGFloat ) {
308- guard let stackView = stackView else { return }
329+ guard let stackView = stackView, let contentView = contentView else { return }
309330
310331 if stackView. axis == . vertical {
311332 contentView. width ( constant: nil )
@@ -317,8 +338,8 @@ open class ScrollStackRow: UIView, UIGestureRecognizerDelegate {
317338 }
318339
319340 private func setupRowSizeToFitLayout( ) {
320- guard let stackView = stackView else { return }
321-
341+ guard let stackView = stackView, let contentView = contentView else { return }
342+
322343 var bestSize : CGSize !
323344 if stackView. axis == . vertical {
324345 let maxAllowedSize = CGSize ( width: stackView. bounds. size. width, height: CGFloat . greatestFiniteMagnitude)
@@ -355,7 +376,7 @@ open class ScrollStackRow: UIView, UIGestureRecognizerDelegate {
355376
356377 open override func touchesBegan( _ touches: Set < UITouch > , with event: UIEvent ? ) {
357378 super. touchesBegan ( touches, with: event)
358- guard contentView. isUserInteractionEnabled else {
379+ guard contentView? . isUserInteractionEnabled ?? false else {
359380 return
360381 }
361382
@@ -367,7 +388,7 @@ open class ScrollStackRow: UIView, UIGestureRecognizerDelegate {
367388
368389 open override func touchesMoved( _ touches: Set < UITouch > , with event: UIEvent ? ) {
369390 super. touchesMoved ( touches, with: event)
370- guard contentView. isUserInteractionEnabled, let touch = touches. first else {
391+ guard contentView? . isUserInteractionEnabled ?? false , let touch = touches. first else {
371392 return
372393 }
373394
@@ -382,7 +403,7 @@ open class ScrollStackRow: UIView, UIGestureRecognizerDelegate {
382403
383404 open override func touchesCancelled( _ touches: Set < UITouch > , with event: UIEvent ? ) {
384405 super. touchesCancelled ( touches, with: event)
385- guard contentView. isUserInteractionEnabled else {
406+ guard contentView? . isUserInteractionEnabled ?? false else {
386407 return
387408 }
388409
@@ -394,7 +415,7 @@ open class ScrollStackRow: UIView, UIGestureRecognizerDelegate {
394415
395416 open override func touchesEnded( _ touches: Set < UITouch > , with event: UIEvent ? ) {
396417 super. touchesEnded ( touches, with: event)
397- guard contentView. isUserInteractionEnabled else {
418+ guard contentView? . isUserInteractionEnabled ?? false else {
398419 return
399420 }
400421
0 commit comments