Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix retain circle, add .distribution support #7

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class LayoutModesView: BaseView {
super.init()

stackView.direction(.column).define { (stack) in
stack.spacing(4) // only for .distribution
stack.addItem(BasicView(text: "View 1"))
stack.addItem(BasicView(text: "View 2"))
stack.addItem(BasicView(text: "View 3"))
Expand Down Expand Up @@ -114,7 +115,8 @@ class LayoutModesView: BaseView {
case .end: stackView.justifyContent(.spaceBetween)
case .spaceBetween: stackView.justifyContent(.spaceAround)
case .spaceAround: stackView.justifyContent(.spaceEvenly)
case .spaceEvenly: stackView.justifyContent(.start)
case .spaceEvenly: stackView.justifyContent(.distribution)
case .distribution: stackView.justifyContent(.start)
}

applyChange()
Expand Down Expand Up @@ -152,6 +154,7 @@ class LayoutModesView: BaseView {
case .spaceBetween: justifyLabel.text = ".spaceBetween"
case .spaceAround: justifyLabel.text = ".spaceAround"
case .spaceEvenly: justifyLabel.text = ".spaceEvenly"
case .distribution: justifyLabel.text = ".distribution"
}

switch stackView.getAlignItems() {
Expand All @@ -161,6 +164,7 @@ class LayoutModesView: BaseView {
case .end: alignItemsLabel.text = ".end"
}

justifyLabel.sizeToFit()
buttonsStackView.markDirty()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,7 @@ class UnitTestsView: BaseView {
case .spaceBetween: stackView.justifyContent(.spaceAround)
case .spaceAround: stackView.justifyContent(.spaceEvenly)
case .spaceEvenly: stackView.justifyContent(.start)
case .distribution: stackView.justifyContent(.distribution)
}

setNeedsLayout()
Expand Down
6 changes: 5 additions & 1 deletion Sources/Impl/Container.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import UIKit

class Container {
fileprivate let stackView: StackView
unowned fileprivate let stackView: StackView
var width: CGFloat?
var height: CGFloat?
var innerWidth: CGFloat?
Expand Down Expand Up @@ -75,6 +75,8 @@ class Container {

var mainAxisTotalItemsLength: CGFloat = 0

var mainAxisTotalItemsMargin: CGFloat = 0

init(_ stackView: StackView) {
self.stackView = stackView
}
Expand Down Expand Up @@ -128,6 +130,7 @@ class Container {

items.forEach({ (item) in
mainAxisTotalItemsLength += item.mainAxisStartMargin
mainAxisTotalItemsMargin += item.mainAxisStartMargin

if direction == .column {
mainAxisTotalItemsLength += (item.height != nil ? item.height! : 0)
Expand All @@ -136,6 +139,7 @@ class Container {
}

mainAxisTotalItemsLength += item.mainAxisEndMargin
mainAxisTotalItemsMargin += item.mainAxisEndMargin
})
}

Expand Down
6 changes: 3 additions & 3 deletions Sources/Impl/ItemInfo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
import UIKit

class ItemInfo {
var view: UIView
var stackItem: StackItemImpl
var container: Container
unowned let view: UIView
unowned let stackItem: StackItemImpl
unowned let container: Container

private var _width: CGFloat?
var width: CGFloat? {
Expand Down
2 changes: 1 addition & 1 deletion Sources/Impl/StackItemImpl.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ enum MeasureType {


class StackItemImpl: NSObject, StackItem {
internal let view: UIView
unowned internal let view: UIView

internal var width: Value?
internal var minWidth: Value?
Expand Down
16 changes: 16 additions & 0 deletions Sources/Impl/StackView+Layout.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,20 @@ extension StackView {

private func adjustItemsSizeToContainer(container: Container) {
guard let mainAxisInnerLength = container.mainAxisInnerLength else { return }

if self.justifyContent == .distribution {
let count = CGFloat(container.items.count)
let gap = max(count - 1, 0) * (_spacing?.value ?? 0) + container.mainAxisTotalItemsMargin
let len = (mainAxisInnerLength - gap) / count
for item in container.items {
if self.direction == .column {
item.height = len
} else {
item.width = len
}
}
return
}

var previousLength: CGFloat?
var lengthDiff = mainAxisInnerLength - container.mainAxisTotalItemsLength
Expand Down Expand Up @@ -164,6 +178,8 @@ extension StackView {
case .spaceEvenly:
betweenSpacing = (containerMainAxisInnner - mainAxisTotalItemsLength) / CGFloat(container.items.count + 1)
startEndSpacing = betweenSpacing
case .distribution:
betweenSpacing = _spacing?.value ?? 0
}
}

Expand Down
21 changes: 21 additions & 0 deletions Sources/StackView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ public class StackView: UIView {
internal var _paddingBottom: Value?
internal var _paddingRight: Value?
internal var _paddingEnd: Value?

internal var _spacing: Value?

public override init(frame: CGRect) {
super.init(frame: frame)
Expand Down Expand Up @@ -322,6 +324,25 @@ public class StackView: UIView {
return self
}

//
// MARK: Spacing
//

/**
The `spacing` property only for justify .distribution in the StackView.

- Parameter value: Default value is 0
*/
@discardableResult
public func spacing(_ spacing: CGFloat) -> StackView {
_spacing = Value(spacing)
return self
}

public func getSpacing() -> CGFloat {
return _spacing?.value ?? 0
}

//
// Layout view
//
Expand Down
2 changes: 2 additions & 0 deletions Sources/Types.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ public enum SJustifyContent {
case spaceAround
/// Items are positioned with space before, between, and after items
case spaceEvenly
/// Items are adjusted to same length in main axis, with space and margin between them
case distribution
}

/**
Expand Down