Fix stack percent defect
This commit is contained in:
parent
2c613ebd09
commit
40da48bc6e
@ -33,8 +33,9 @@ open class Stack<T>: Container where T: StackModelProtocol {
|
||||
})
|
||||
|
||||
// Adds the views
|
||||
let totalSpace = getTotalSpace()
|
||||
for (index, view) in stackItems.enumerated() {
|
||||
addView(view, stackModel.molecules[index], percentModifier: getPercentModifier(), lastItem: lastItemIndex == index)
|
||||
addView(view, stackModel.molecules[index], totalSpacing: totalSpace, lastItem: lastItemIndex == index)
|
||||
}
|
||||
}
|
||||
|
||||
@ -173,14 +174,12 @@ open class Stack<T>: Container where T: StackModelProtocol {
|
||||
|
||||
// MARK: - Adding to stack
|
||||
/// Gets the percent modifier. This value is used to help properly calculate percent for stack items when spacing is involved.
|
||||
private func getPercentModifier() -> CGFloat {
|
||||
private func getTotalSpace() -> CGFloat {
|
||||
guard let stackModel = stackModel else { return 0.0 }
|
||||
var totalSpace: CGFloat = 0.0
|
||||
var totalViews = 0
|
||||
var firstMoleculeFound = false
|
||||
for stackItemModel in stackModel.molecules {
|
||||
guard !stackItemModel.gone else { continue }
|
||||
totalViews += 1
|
||||
let spacing = stackItemModel.spacing ?? stackModel.spacing
|
||||
if firstMoleculeFound {
|
||||
totalSpace += spacing
|
||||
@ -189,11 +188,11 @@ open class Stack<T>: Container where T: StackModelProtocol {
|
||||
totalSpace += (stackModel.useStackSpacingBeforeFirstItem ? spacing : stackItemModel.spacing ?? 0)
|
||||
}
|
||||
}
|
||||
return (totalViews > 0 ? -(totalSpace / CGFloat(totalViews)) : 0)
|
||||
return totalSpace
|
||||
}
|
||||
|
||||
/// Adds the stack item view
|
||||
private func addView(_ view: UIView,_ model: StackItemModelProtocol, percentModifier: CGFloat, lastItem: Bool) {
|
||||
private func addView(_ view: UIView,_ model: StackItemModelProtocol, totalSpacing: CGFloat, lastItem: Bool) {
|
||||
guard let stackModel = self.stackModel else { return }
|
||||
guard !model.gone else {
|
||||
// Gone views do not show
|
||||
@ -223,7 +222,9 @@ open class Stack<T>: Container where T: StackModelProtocol {
|
||||
pinView(view, toView: contentView, attribute: .leading, relation: .equal, priority: .required, constant: 0)
|
||||
pinView(contentView, toView: view, attribute: .trailing, relation: .equal, priority: .required, constant: 0)
|
||||
if let percent = model.percent {
|
||||
view.heightAnchor.constraint(equalTo: contentView.heightAnchor, multiplier: CGFloat(percent)/100.0, constant: percentModifier).isActive = true
|
||||
let multiplier = CGFloat(percent)/100.0
|
||||
let constant = multiplier * totalSpacing
|
||||
view.heightAnchor.constraint(equalTo: contentView.heightAnchor, multiplier: multiplier, constant: -constant).isActive = true
|
||||
}
|
||||
if lastItem {
|
||||
pinView(contentView, toView: view, attribute: .bottom, relation: .equal, priority: .required, constant: 0)
|
||||
@ -240,7 +241,9 @@ open class Stack<T>: Container where T: StackModelProtocol {
|
||||
pinView(view, toView: contentView, attribute: .top, relation: .equal, priority: .required, constant: 0)
|
||||
pinView(contentView, toView: view, attribute: .bottom, relation: .equal, priority: .required, constant: 0)
|
||||
if let percent = model.percent {
|
||||
view.widthAnchor.constraint(equalTo: contentView.widthAnchor, multiplier: CGFloat(percent)/100.0, constant: percentModifier).isActive = true
|
||||
let multiplier = CGFloat(percent)/100.0
|
||||
let constant = multiplier * totalSpacing
|
||||
view.widthAnchor.constraint(equalTo: contentView.widthAnchor, multiplier: multiplier, constant: -constant).isActive = true
|
||||
}
|
||||
if lastItem {
|
||||
pinView(contentView, toView: view, attribute: .right, relation: .equal, priority: .required, constant: 0)
|
||||
|
||||
Loading…
Reference in New Issue
Block a user