Skip to content
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
48 changes: 48 additions & 0 deletions Classes/Extensions/View+Spacing.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#if os(macOS)
import AppKit
#else
import UIKit
#endif

private var spacingAssociationKey: UInt8 = 0

extension BaseView {
@available(iOS 11.0, *)
var spacing: State<CGFloat>? {
get {
return objc_getAssociatedObject(self, &spacingAssociationKey) as? State<CGFloat>
}
set(newValue) {
objc_setAssociatedObject(self, &spacingAssociationKey, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN)
}
}

@available(iOS 11.0, *)
@discardableResult
public func customSpacing(_ spacing: CGFloat) -> Self {
if self.spacing == nil {
self.spacing = State.init(wrappedValue: spacing)
}
else {
self.spacing?.wrappedValue = spacing
}

if let superView = self.superview as? _STV {
superView.setCustomSpacing(spacing, after: self)
}
return self
}

@available(iOS 11.0, *)
@discardableResult
public func customSpacing(_ spacing: State<CGFloat>) -> Self {
self.spacing = spacing
spacing.listen { [weak self] new in
guard let self = self else { return }
if let superView = self.superview as? _STV {
superView.setCustomSpacing(new, after: self)
}
}
return self
}
}
11 changes: 10 additions & 1 deletion Classes/Views/Universal/StackView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,16 @@ open class _StackView: _STV, AnyDeclarativeProtocol, DeclarativeProtocolInternal
self.spacing = state.wrappedValue
return self
}


open override func addArrangedSubview(_ view: UIView) {
super.addArrangedSubview(view)
if #available(iOS 11.0, *) {
if let spacing = view.spacing {
self.setCustomSpacing(spacing.wrappedValue, after: view)
}
}
}

func add(item: BodyBuilderItemable) {
switch item.bodyBuilderItem {
case .single(let view):
Expand Down