Skip to content

Commit

Permalink
Fixed wrong calculation in sizeThatFits for .center and .equal in hor…
Browse files Browse the repository at this point in the history
…izontal mode
  • Loading branch information
kennic committed Jan 28, 2021
1 parent dc9cf9f commit c3eecd7
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 32 deletions.
2 changes: 1 addition & 1 deletion FrameLayoutKit.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = 'FrameLayoutKit'
s.version = '5.2.4'
s.version = '5.2.5'
s.summary = 'FrameLayoutKit is a super fast and easy to use layout kit'
s.description = <<-DESC
An auto layout kit helps you to layout your UI easier, faster and more effective
Expand Down
46 changes: 15 additions & 31 deletions FrameLayoutKit/Classes/StackFrameLayout.swift
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ open class StackFrameLayout: FrameLayout {
var maxHeight: CGFloat = 0

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

break

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

for frameLayout in visibleFrameLayouts {
if frameLayout.isFlexible {
flexibleFrames.append(frameLayout)
continue
}

frameContentSize = frameLayout.sizeThatFits(frameContentSize)
visibleFrameLayouts.forEach {
frameContentSize = CGSize(width: cellWidth, height: contentSize.height).limitTo(minSize: $0.minSize, maxSize: $0.maxSize)
frameContentSize = $0.sizeThatFits(frameContentSize)

gapSpace = frameContentSize.width > 0 && frameLayout != lastFrameLayout ? spacing : 0
gapSpace = frameContentSize.width > 0 && $0 != lastFrameLayout ? spacing : 0
totalSpace += frameContentSize.width + gapSpace
maxHeight = max(maxHeight, frameContentSize.height)
}

let flexibleFrameCount = flexibleFrames.count
if flexibleFrameCount > 0 {
let remainingSpace = CGFloat(flexibleFrameCount - 1) * spacing
let remainingWidth = contentSize.width - totalSpace - remainingSpace
let cellWidth = remainingWidth / CGFloat(flexibleFrameCount)

flexibleFrames.forEach {
frameContentSize = CGSize(width: cellWidth, height: contentSize.height)
frameContentSize = $0.sizeThatFits(frameContentSize)

totalSpace += frameContentSize.width
maxHeight = max(maxHeight, frameContentSize.height)
}
}

break

case .split(let ratio):
Expand Down Expand Up @@ -762,11 +745,11 @@ open class StackFrameLayout: FrameLayout {
let visibleFrameLayouts = visibleFrames()
let visibleFrameCount = visibleFrameLayouts.count
let spaces = CGFloat(visibleFrameCount - 1) * spacing
let cellSize = (containerFrame.width - spaces) / CGFloat(Float(visibleFrameCount))
let cellWidth = (containerFrame.width - spaces) / CGFloat(Float(visibleFrameCount))

if isOverlapped {
for frameLayout in frameLayouts {
frameContentSize = frameLayout.isFlexible ? containerFrame.size : CGSize(width: cellSize, height: containerFrame.height).limitTo(minSize: frameLayout.minSize, maxSize: frameLayout.maxSize)
frameContentSize = frameLayout.isFlexible ? containerFrame.size : CGSize(width: cellWidth, height: containerFrame.height).limitTo(minSize: frameLayout.minSize, maxSize: frameLayout.maxSize)

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

for frameLayout in frameLayouts {
frameContentSize = CGSize(width: cellSize, height: containerFrame.height)
frameContentSize = CGSize(width: cellWidth, height: containerFrame.height).limitTo(minSize: frameLayout.minSize, maxSize: frameLayout.maxSize)
targetFrame.origin.x = containerFrame.minX + usedSpace
targetFrame.size.width = frameContentSize.width
frameLayout.frame = targetFrame

if frameLayout.isEmpty { continue }

usedSpace += frameContentSize.width + spacing
gapSpace = frameContentSize.width > 0 ? spacing : 0
usedSpace += frameContentSize.width + gapSpace
}
break

Expand Down

0 comments on commit c3eecd7

Please sign in to comment.