11import UIKit
22
3- open class HStackView : UIView {
3+ open class HStackView : UIView , StackView {
44
55 public var alignment : HStackAlignment = . center
66 public var distribution : HStackDistribution = . autoSpacing
7+ public var padding : UIEdgeInsets = . zero
78
89 public required init (
910 alignment: HStackAlignment = . center,
1011 distribution: HStackDistribution = . spacing( 2 ) ,
12+ padding: UIEdgeInsets = . zero,
1113 @_StackKitHStackContentResultBuilder content: ( ) -> [ UIView ] = { [ ] }
1214 ) {
1315 super. init ( frame: . zero)
1416
1517 self . alignment = alignment
1618 self . distribution = distribution
19+ self . padding = padding
1720
1821 for v in content ( ) {
1922 addSubview ( v)
@@ -75,7 +78,7 @@ open class HStackView: UIView {
7578 let w = effectiveSubviews. map ( { $0. bounds } ) . reduce ( CGRect . zero) { result, rect in
7679 result. union ( rect)
7780 } . height
78- return CGSize ( width: h, height: w)
81+ return CGSize ( width: h + paddingRight , height: w + paddingVertically )
7982 }
8083
8184 open func hideIfNoEffectiveViews( ) {
@@ -95,11 +98,11 @@ open class HStackView: UIView {
9598
9699 switch alignment {
97100 case . top:
98- effectiveSubviews. forEach { $0. frame. origin. y = 0 }
101+ effectiveSubviews. forEach { $0. frame. origin. y = _stackContentRect . minY }
99102 case . center:
100- effectiveSubviews. forEach { $0. center. y = frame . height / 2 }
103+ effectiveSubviews. forEach { $0. center. y = _stackContentRect . midY }
101104 case . bottom:
102- effectiveSubviews. forEach { $0. frame. origin. y = frame . height - $0. frame. height }
105+ effectiveSubviews. forEach { $0. frame. origin. y = _stackContentRect . maxY - $0. frame. height }
103106 }
104107
105108 switch distribution {
@@ -153,26 +156,6 @@ open class HStackView: UIView {
153156 }
154157}
155158
156- extension HStackView {
157-
158- private func spacerViews( ) -> [ SpacerView ] {
159- effectiveSubviews. compactMap ( { $0 as? SpacerView } )
160- }
161- private func dynamicSpacerViews( ) -> [ SpacerView ] {
162- effectiveSubviews. compactMap ( { $0 as? SpacerView } ) . filter ( { $0. length == . greatestFiniteMagnitude } )
163- }
164- private func dividerViews( ) -> [ DividerView ] {
165- effectiveSubviews. compactMap ( { $0 as? DividerView } )
166- }
167-
168- private func viewsWithoutSpacer( ) -> [ UIView ] {
169- effectiveSubviews. filter ( { ( $0 as? SpacerView ) == nil } )
170- }
171- private func viewsWithoutSpacerAndDivider( ) -> [ UIView ] {
172- effectiveSubviews. filter ( { ( $0 as? SpacerView ) == nil && ( $0 as? DividerView ) == nil } )
173- }
174- }
175-
176159extension HStackView {
177160
178161 /// 自动间距
@@ -185,9 +168,9 @@ extension HStackView {
185168 ///
186169 private func autoSpacing( ) -> CGFloat {
187170 let unspacerViews = viewsWithoutSpacer ( )
188- let spacersCount = spacerViews ( ) . map ( { isSpacerBetweenViews ( $0) } ) . filter ( { $0 } ) . count
171+ let spacersCount = spacerViews ( ) . map ( { isSpacerBetweenInTwoViews ( spacerView : $0) } ) . filter ( { $0 } ) . count
189172 let number = unspacerViews. count - spacersCount - 1
190- return ( frame. width - viewsWidth( ) - spacerSpecifyLength ( ) ) / CGFloat( max ( 1 , number) )
173+ return ( frame. width - viewsWidth( ) - lengthOfAllFixedLengthSpacer ( ) ) / CGFloat( max ( 1 , number) )
191174 }
192175
193176 private func viewsWidth( ) -> CGFloat {
@@ -197,7 +180,7 @@ extension HStackView {
197180 private func makeSpacing( _ spacing: CGFloat ) {
198181 for (index, subview) in effectiveSubviews. enumerated ( ) {
199182 if index == 0 {
200- subview. frame. origin. x = 0
183+ subview. frame. origin. x = _stackContentRect . minX
201184 } else {
202185 let previousView = effectiveSubviews [ index - 1 ]
203186 if ( previousView as? SpacerView ) != nil || ( subview as? SpacerView ) != nil { // spacer and view no spacing
@@ -215,18 +198,18 @@ extension HStackView {
215198 }
216199 for subview in effectiveSubviews {
217200 let oldHeight = subview. frame. height
218- subview. frame. size. height = frame . height
201+ subview. frame. size. height = _stackContentWidth
219202
220203 // fix #https://github.com/iWECon/StackKit/issues/21
221204 guard alignment == . center else {
222205 continue
223206 }
224- subview. frame. origin. y -= ( frame . height - oldHeight) / 2
207+ subview. frame. origin. y -= ( _stackContentWidth - oldHeight) / 2
225208 }
226209 }
227210
228211 private func fillWidth( ) {
229- let maxW = frame. width - spacerSpecifyLength ( ) - dividerSpecifyLength( )
212+ let maxW = frame. width - lengthOfAllFixedLengthSpacer ( ) - dividerSpecifyLength( )
230213 var w = ( maxW) / CGFloat( viewsWithoutSpacerAndDivider ( ) . count)
231214
232215 let unspacersView = viewsWithoutSpacerAndDivider ( )
@@ -265,27 +248,6 @@ extension HStackView {
265248// MARK: Spacer
266249extension HStackView {
267250
268- // 取出固定 length 的 spacer
269- private func spacerSpecifyLength( ) -> CGFloat {
270- spacerViews ( )
271- . map ( { $0. setLength } )
272- . reduce ( 0 , + )
273- }
274-
275- private func isSpacerBetweenViews( _ spacer: SpacerView ) -> Bool {
276- guard let index = effectiveSubviews. firstIndex ( of: spacer) else {
277- return false
278- }
279-
280- guard effectiveSubviews. count >= 3 else {
281- return false
282- }
283-
284- let start : Int = 1
285- let end : Int = effectiveSubviews. count - 2
286- return ( start ... end) . contains ( index)
287- }
288-
289251 private func fillSpecifySpacer( ) {
290252 let spacers = effectiveSubviews. compactMap ( { $0 as? SpacerView } )
291253 for spacer in spacers {
@@ -302,7 +264,7 @@ extension HStackView {
302264 guard unspacerViews. count != effectiveSubviews. count else { return }
303265
304266 // 在 view 与 view 之间的 spacer view 数量: 两个 view 夹一个 spacer view
305- let betweenInViewsCount = spacerViews ( ) . map ( { isSpacerBetweenViews ( $0) } ) . filter ( { $0 } ) . count
267+ let betweenInViewsCount = spacerViews ( ) . map ( { isSpacerBetweenInTwoViews ( spacerView : $0) } ) . filter ( { $0 } ) . count
306268 // 非 spacer view 的总宽度
307269 let unspacerViewsWidth = viewsWidth ( )
308270 // 排除 spacer view 后的间距
@@ -329,7 +291,7 @@ extension HStackView {
329291
330292 // 非 spacerView 的所有宽度
331293 let unspacerViewsMaxWidth = unspacerViewsWidth + unspacerViewsSpacing
332- let spacersWidth = ( frame. width - unspacerViewsMaxWidth - self . spacerSpecifyLength ( ) )
294+ let spacersWidth = ( frame. width - unspacerViewsMaxWidth - self . lengthOfAllFixedLengthSpacer ( ) )
333295 let spacerWidth = spacersWidth / CGFloat( self . dynamicSpacerViews ( ) . count)
334296
335297 let spacerViews = self . spacerViews ( )
0 commit comments