Skip to content

Commit c3eecd7

Browse files
committed
Fixed wrong calculation in sizeThatFits for .center and .equal in horizontal mode
1 parent dc9cf9f commit c3eecd7

2 files changed

Lines changed: 16 additions & 32 deletions

File tree

FrameLayoutKit.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Pod::Spec.new do |s|
22
s.name = 'FrameLayoutKit'
3-
s.version = '5.2.4'
3+
s.version = '5.2.5'
44
s.summary = 'FrameLayoutKit is a super fast and easy to use layout kit'
55
s.description = <<-DESC
66
An auto layout kit helps you to layout your UI easier, faster and more effective

FrameLayoutKit/Classes/StackFrameLayout.swift

Lines changed: 15 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,7 @@ open class StackFrameLayout: FrameLayout {
378378
var maxHeight: CGFloat = 0
379379

380380
switch distribution {
381-
case .left, .right, .top, .bottom:
381+
case .left, .right, .top, .bottom, .center:
382382
var flexibleFrames = [FrameLayout]()
383383
for frameLayout in activeFrameLayouts {
384384
if frameLayout.isEmpty { continue }
@@ -418,39 +418,22 @@ open class StackFrameLayout: FrameLayout {
418418

419419
break
420420

421-
case .equal, .center:
421+
case .equal:
422422
let visibleFrameLayouts = visibleFrames()
423-
var flexibleFrames = [FrameLayout]()
424-
frameContentSize = CGSize(width: contentSize.width / CGFloat(visibleFrameLayouts.count), height: contentSize.height)
423+
let visibleFrameCount = visibleFrameLayouts.count
424+
let spaces = CGFloat(visibleFrameCount - 1) * spacing
425+
let contentWidth = contentSize.width - spaces
426+
let cellWidth = contentWidth / CGFloat(visibleFrameCount)
425427

426-
for frameLayout in visibleFrameLayouts {
427-
if frameLayout.isFlexible {
428-
flexibleFrames.append(frameLayout)
429-
continue
430-
}
431-
432-
frameContentSize = frameLayout.sizeThatFits(frameContentSize)
428+
visibleFrameLayouts.forEach {
429+
frameContentSize = CGSize(width: cellWidth, height: contentSize.height).limitTo(minSize: $0.minSize, maxSize: $0.maxSize)
430+
frameContentSize = $0.sizeThatFits(frameContentSize)
433431

434-
gapSpace = frameContentSize.width > 0 && frameLayout != lastFrameLayout ? spacing : 0
432+
gapSpace = frameContentSize.width > 0 && $0 != lastFrameLayout ? spacing : 0
435433
totalSpace += frameContentSize.width + gapSpace
436434
maxHeight = max(maxHeight, frameContentSize.height)
437435
}
438436

439-
let flexibleFrameCount = flexibleFrames.count
440-
if flexibleFrameCount > 0 {
441-
let remainingSpace = CGFloat(flexibleFrameCount - 1) * spacing
442-
let remainingWidth = contentSize.width - totalSpace - remainingSpace
443-
let cellWidth = remainingWidth / CGFloat(flexibleFrameCount)
444-
445-
flexibleFrames.forEach {
446-
frameContentSize = CGSize(width: cellWidth, height: contentSize.height)
447-
frameContentSize = $0.sizeThatFits(frameContentSize)
448-
449-
totalSpace += frameContentSize.width
450-
maxHeight = max(maxHeight, frameContentSize.height)
451-
}
452-
}
453-
454437
break
455438

456439
case .split(let ratio):
@@ -762,11 +745,11 @@ open class StackFrameLayout: FrameLayout {
762745
let visibleFrameLayouts = visibleFrames()
763746
let visibleFrameCount = visibleFrameLayouts.count
764747
let spaces = CGFloat(visibleFrameCount - 1) * spacing
765-
let cellSize = (containerFrame.width - spaces) / CGFloat(Float(visibleFrameCount))
748+
let cellWidth = (containerFrame.width - spaces) / CGFloat(Float(visibleFrameCount))
766749

767750
if isOverlapped {
768751
for frameLayout in frameLayouts {
769-
frameContentSize = frameLayout.isFlexible ? containerFrame.size : CGSize(width: cellSize, height: containerFrame.height).limitTo(minSize: frameLayout.minSize, maxSize: frameLayout.maxSize)
752+
frameContentSize = frameLayout.isFlexible ? containerFrame.size : CGSize(width: cellWidth, height: containerFrame.height).limitTo(minSize: frameLayout.minSize, maxSize: frameLayout.maxSize)
770753

771754
targetFrame.origin.x = containerFrame.minX
772755
targetFrame.size.width = frameContentSize.width
@@ -777,14 +760,15 @@ open class StackFrameLayout: FrameLayout {
777760
}
778761

779762
for frameLayout in frameLayouts {
780-
frameContentSize = CGSize(width: cellSize, height: containerFrame.height)
763+
frameContentSize = CGSize(width: cellWidth, height: containerFrame.height).limitTo(minSize: frameLayout.minSize, maxSize: frameLayout.maxSize)
781764
targetFrame.origin.x = containerFrame.minX + usedSpace
782765
targetFrame.size.width = frameContentSize.width
783766
frameLayout.frame = targetFrame
784767

785768
if frameLayout.isEmpty { continue }
786769

787-
usedSpace += frameContentSize.width + spacing
770+
gapSpace = frameContentSize.width > 0 ? spacing : 0
771+
usedSpace += frameContentSize.width + gapSpace
788772
}
789773
break
790774

0 commit comments

Comments
 (0)