@@ -30,6 +30,7 @@ open class StackScrollView: UICollectionView, UICollectionViewDataSource, UIColl
3030 static let left = " me.muukii.StackScrollView.left "
3131 static let bottom = " me.muukii.StackScrollView.bottom "
3232 static let width = " me.muukii.StackScrollView.width "
33+ static let height = " me.muukii.StackScrollView.height "
3334 }
3435
3536 private static func defaultLayout( ) -> UICollectionViewFlowLayout {
@@ -51,6 +52,10 @@ open class StackScrollView: UICollectionView, UICollectionViewDataSource, UIColl
5152 didSet {
5253 }
5354 }
55+
56+ private var direction : UICollectionView . ScrollDirection {
57+ ( collectionViewLayout as! UICollectionViewFlowLayout ) . scrollDirection
58+ }
5459
5560 // MARK: - Initializers
5661
@@ -323,27 +328,61 @@ open class StackScrollView: UICollectionView, UICollectionViewDataSource, UIColl
323328
324329 if let view = view as? ManualLayoutStackCellType {
325330
326- return view. size ( maxWidth: collectionView. bounds. width, maxHeight: nil )
331+ switch direction {
332+ case . vertical:
333+ return view. size ( maxWidth: collectionView. bounds. width, maxHeight: nil )
334+ case . horizontal:
335+ return view. size ( maxWidth: nil , maxHeight: collectionView. bounds. height)
336+ @unknown default :
337+ fatalError ( )
338+ }
327339
328340 } else {
329341
330- let width : NSLayoutConstraint = {
342+ switch direction {
343+ case . vertical:
331344
332- guard let c = view. constraints. filter ( { $0. identifier == LayoutKeys . width } ) . first else {
333- let width = view. widthAnchor. constraint ( equalToConstant: collectionView. bounds. width)
334- width. identifier = LayoutKeys . width
335- width. isActive = true
336- return width
337- }
345+ let width : NSLayoutConstraint = {
338346
339- return c
340- } ( )
347+ guard let c = view. constraints. first ( where: { $0. identifier == LayoutKeys . width } ) else {
348+ let width = view. widthAnchor. constraint ( equalToConstant: collectionView. bounds. width)
349+ width. identifier = LayoutKeys . width
350+ width. isActive = true
351+ return width
352+ }
341353
342- width. constant = collectionView. bounds. width
354+ return c
355+ } ( )
343356
344- let size = view. superview? . systemLayoutSizeFitting ( UIView . layoutFittingCompressedSize) ?? view. systemLayoutSizeFitting ( UIView . layoutFittingCompressedSize)
345-
346- return size
357+ width. constant = collectionView. bounds. width
358+
359+ let size = view. superview? . systemLayoutSizeFitting ( UIView . layoutFittingCompressedSize) ?? view. systemLayoutSizeFitting ( UIView . layoutFittingCompressedSize)
360+
361+ return size
362+
363+ case . horizontal:
364+
365+ let heightConstraint : NSLayoutConstraint = {
366+
367+ guard let c = view. constraints. first ( where: { $0. identifier == LayoutKeys . height } ) else {
368+ let heightConstraint = view. heightAnchor. constraint ( equalToConstant: collectionView. bounds. height)
369+ heightConstraint. identifier = LayoutKeys . height
370+ heightConstraint. isActive = true
371+ return heightConstraint
372+ }
373+
374+ return c
375+ } ( )
376+
377+ heightConstraint. constant = collectionView. bounds. width
378+
379+ let size = view. superview? . systemLayoutSizeFitting ( UIView . layoutFittingCompressedSize) ?? view. systemLayoutSizeFitting ( UIView . layoutFittingCompressedSize)
380+
381+ return size
382+
383+ @unknown default :
384+ fatalError ( )
385+ }
347386
348387 }
349388 }
0 commit comments