Skip to content

Commit

Permalink
Added func return container from stack, fix bug axis
Browse files Browse the repository at this point in the history
  • Loading branch information
exxcqz committed Apr 26, 2022
1 parent 1a757e6 commit cd85680
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 0 deletions.
Binary file removed .DS_Store
Binary file not shown.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,4 @@ fastlane/report.xml
fastlane/Preview.html
fastlane/screenshots
fastlane/test_output
.DS_Store
Binary file removed Example/.DS_Store
Binary file not shown.
55 changes: 55 additions & 0 deletions Sources/Extensions/Array+Stack.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public extension Collection where Iterator.Element: UIView, Self.Index == Int {

func stack(axis: StackAxis, spacing: Number = 0.0, state: AnyHashable = DEFAULT_STATE) {
for view in self {
view.frame = .zero
guard view.superview != nil else {
assertionFailure("Can not configure stack relation without superview.")
return
Expand Down Expand Up @@ -79,4 +80,58 @@ public extension Collection where Iterator.Element: UIView, Self.Index == Int {

}
}

func stackToContainer(in view: UIView, axis: StackAxis, spacing: Number = 0.0, state: AnyHashable = DEFAULT_STATE, installerBlock: (ViewMaker) -> Void) -> UIView {
let containerView = UIView()
view.addSubview(containerView)
Maker.configure(view: containerView, for: state, installerBlock: installerBlock)

for view in self {
view.frame = .zero
containerView.addSubview(view)
}

guard containerView.nx_state == state else {
return containerView
}

let count = CGFloat(self.count)
var prevView = self[0]

switch axis {
case .horizontal:
let width = (containerView.bounds.width - (count - 1) * spacing.value) / count
let height = containerView.bounds.height

for (index, view) in self.enumerated() {
view.configureFrame { maker in
maker.size(width: width, height: height)
if index == 0 {
maker.left()
}
else {
maker.left(to: prevView.nui_right, inset: spacing)
}
}
prevView = view
}
case .vertical:
let width = containerView.bounds.width
let height = (containerView.bounds.height - (count - 1) * spacing.value) / count

for (index, view) in self.enumerated() {
view.configureFrame { maker in
maker.size(width: width, height: height)
if index == 0 {
maker.top()
}
else {
maker.top(to: prevView.nui_bottom, inset: spacing)
}
}
prevView = view
}
}
return containerView
}
}

0 comments on commit cd85680

Please sign in to comment.