Мы пытаемся создать GridView с помощью SwiftUI , мы получаем проблему, компилятор хочет разбить выражение, но мы не знаем, где именно оно происходит. Следующий код взят из https://github.com/johnsusek/FlowStack
. Нет поддержки для решения этой проблемы. Мы хотели, чтобы это работало.
[![import SwiftUI
@available(iOS 13.0, OSX 10.15, tvOS 13.0, watchOS 6.0, *)
public struct FlowStack<Content>: View where Content: View {
// The number of columns we want to display
var columns: Int
// The total number of items in the stack
var numItems: Int
// The alignment of our columns in the last row
// when they don't fill all the column slots
var alignment: HorizontalAlignment
public let content: (Int, CGFloat) -> Content
public init(
columns: Int,
numItems: Int,
alignment: HorizontalAlignment?,
@ViewBuilder content: @escaping (Int, CGFloat) -> Content) {
self.content = content
self.columns = columns
self.numItems = numItems
self.alignment = alignment ?? HorizontalAlignment.leading
}
public var body : some View {
// A GeometryReader is required to size items in the scroll view
GeometryReader { geometry in
let frameWidth : CGFloat = geometry.size.width/CGFloat(self.columns)
let contentIndex : Int = (self.numItems / self.columns) * self.columns
// Assume a vertical scrolling orientation for the grid
ScrollView(Axis.Set.vertical) {
// VStacks are our rows
VStack(alignment: self.alignment, spacing: 0) {
let count = (self.numItems / self.columns)
ForEach(0 ..< count) { row in
// HStacks are our columns
HStack(spacing: 0) {
let innerCount = (self.columns - 1)
ForEach(0 ... innerCount) { column in
self.content(
// Pass the index to the content
(row * self.columns) + column,
// Pass the column width to the content
frameWidth
)
// Size the content to frame to fill the column
.frame(width: frameWidth)
}
}
}
// Last row
// HStacks are our columns
HStack(spacing: 0) {
let count = (self.numItems % self.columns)
ForEach(0 ..< count) { column in
self.content(
// Pass the index to the content
contentIndex + column,
// Pass the column width to the content
frameWidth
)
// Size the content to frame to fill the column
.frame(width: frameWidth)
}
}
}
}
}
}
}
] 1 ] 1