我正在 Xcode 的 beta 版本中使用 LazyVGrid,并且尝试使垂直 ScrollView 显示为两列而不是一列。我将此视图设置为包含一个 ScrollView,用于检查 iOS 14 是否可用,如果可用,则将视图呈现为两列网格(我将列放入 LazyVGrid 的此实例中,而不是作为外部属性,因为我没有'不想用 #available 属性标记整个视图 - 尽管也许我应该)。
但是,当我预览视图时,它只显示一列。我的设置中是否有任何内容可能导致我的视图仅显示一列而不是两列?
相关 - 有没有一种简单的方法可以在 Xcode 中预览包含/不包含 iOS 14 的不同版本?(如果我的问题是我的预览版本没有 iOS 14)。
以下是我的看法,供参考:
struct PackPage: View {
@Binding var isPresented: Bool
var allPacks: [Pack] = [samplePack, couplesPack, roadTripPack, familyPack]
var purchasedPacks: [Pack] = [samplePack]
var unpurchasedPacks: [Pack] = [couplesPack, roadTripPack, familyPack]
let userDefaults = UserDefaults.standard
@State private var action: Int? = 0
@State private var linkLock: Pack? = samplePack
private func isPurchased(pack: Pack, allPacks: [Pack]) -> Bool {
if allPacks.contains(pack) {
return true
} else {
return false
}
}
var body: some View {
NavigationView {
VStack {
NavigationLink(destination: PurchasePage(pack: linkLock!), tag: 1, selection: $action) {
EmptyView()
}
ScrollView(.vertical, showsIndicators: false) {
ForEach((allPacks), id: \.self) { pack in
//Checks if iOS version 14.0 is available to render the lazy grid view
if #available(iOS 14.0, *) {
LazyVGrid(columns: [GridItem(.flexible()), GridItem(.flexible())], spacing: 20) {
if
//checks if the pack is in the purchased list - if so, renders it as an unlocked tile.
isPurchased(pack: pack, allPacks: purchasedPacks) {
UnlockedPackTile(tilePack: pack)
.onTapGesture {
print("Originally tapped \(pack.name)")
self.userInformation.defaultPack = pack
self.isPresented.toggle()
}
} else {
// if pack is not purchased, renders it as a locked pack tile
LockedPackTile(tilePackLocked: pack)
.onTapGesture {
self.linkLock = pack
self.action = 1
}
}
}
} else {
//does this as a simple VStack instead if iOS 14 is not available.
if isPurchased(pack: pack, allPacks: purchasedPacks) {
UnlockedPackTile(tilePack: pack)
.onTapGesture {
print("Originally tapped \(pack.name)")
self.userInformation.defaultPack = pack
self.isPresented.toggle()
}
} else {
LockedPackTile(tilePackLocked: pack)
.onTapGesture {
self.linkLock = pack
self.action = 1
}
}
}
}
}
Text("Button")
.navigationBarTitle("Question Packs")
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
你必须放在ForEach里面LazyVGrid,所以后者安排动态视图:
ScrollView(.vertical, showsIndicators: false) {
//Checks if iOS version 14.0 is available to render the lazy grid view
if #available(iOS 14.0, *) {
LazyVGrid(columns: [GridItem(.flexible()), GridItem(.flexible())], spacing: 20) {
ForEach((allPacks), id: \.self) { pack in
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1693 次 |
| 最近记录: |