Skip to content

Commit

Permalink
fixes #1: NewLine() does not work in preview sometimes
Browse files Browse the repository at this point in the history
  • Loading branch information
dkk committed Apr 22, 2021
1 parent 29a7f39 commit 78bb1e2
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 19 deletions.
10 changes: 5 additions & 5 deletions Sources/WrappingHStack/InternalWrappingHStack.swift
Expand Up @@ -2,10 +2,10 @@ import SwiftUI

// based on https://swiftui.diegolavalle.com/posts/linewrapping-stacks/
struct InternalWrappingHStack: View {
var width: CGFloat
var alignment: HorizontalAlignment
var spacing: WrappingHStack.Spacing
var content: [WrappingHStack.ViewType]
let width: CGFloat
let alignment: HorizontalAlignment
let spacing: WrappingHStack.Spacing
let content: [WrappingHStack.ViewType]

var firstItemOfEachLane: [Int] {
return content
Expand Down Expand Up @@ -64,7 +64,7 @@ struct InternalWrappingHStack: View {

var body: some View {
VStack(alignment: alignment, spacing: 0) {
ForEach(0 ..< totalLanes, id: \.self) { laneIndex in
ForEach(0 ..< totalLanes, id: \.self) { laneIndex in
HStack(spacing: 0) {
if alignment == .center || alignment == .trailing, shouldHaveSideSpacers(lane: laneIndex) {
Spacer(minLength: 0)
Expand Down
2 changes: 1 addition & 1 deletion Sources/WrappingHStack/NewLine.swift
Expand Up @@ -2,5 +2,5 @@ import SwiftUI

public struct NewLine: View {
public init() { }
public let body = EmptyView()
public let body = Spacer(minLength: .infinity)
}
6 changes: 3 additions & 3 deletions Sources/WrappingHStack/WrappingHStack.swift
Expand Up @@ -43,9 +43,9 @@ public struct WrappingHStack: View {
}
}

var items: [ViewType]
var alignment: HorizontalAlignment
var spacing: Spacing
let items: [ViewType]
let alignment: HorizontalAlignment
let spacing: Spacing
@State private var height: CGFloat = 0

public var body: some View {
Expand Down
28 changes: 18 additions & 10 deletions WrappingHStackExample/WrappingHStackExample/ContentView.swift
Expand Up @@ -17,17 +17,25 @@ struct ExampleView: View {
.frame(width: 20, height: 20)
.background(Color.purple)

Text("1234567898")
.bold()
NewLine()

Text("NL")

Image(systemName: "face.dashed")
.font(.title)
.border(Color.green)

Text("bcdefghijklmnopqrs")
.font(.title)

WrappingHStack(1...9, id:\.self, alignment: alignment, spacing: spacing) {
Text("Item: \($0)")
.padding(.all, 12)
.background(RoundedRectangle(cornerRadius: 10).stroke())
}.frame(width: 380, height: 150)
}.frame(width: 380)

Text("1234567898")
.bold()
}
}

Expand All @@ -36,22 +44,22 @@ struct ExampleView: View {
switch exampleType {
case .leading:
example(alignment: .leading, spacing: .constant(0))

case .center:
example(alignment: .center, spacing: .constant(0))

case .trailing:
example(alignment: .trailing, spacing: .constant(0))

case .dynamicLeading:
example(alignment: .leading, spacing: .dynamic(minSpacing: 0))

case .dynamicCenter:
example(alignment: .center, spacing: .dynamic(minSpacing: 0))

case .dynamicTrailing:
example(alignment: .trailing, spacing: .dynamic(minSpacing: 0))

case .dynamicIncludingBorders:
example(alignment: .leading, spacing: .dynamicIncludingBorders(minSpacing: 0))
}
Expand Down

0 comments on commit 78bb1e2

Please sign in to comment.