gpt4 book ai didi

ios - LazyVGrid 中的 NavigationLink 循环后面的所有条目,SwiftUI

转载 作者:行者123 更新时间:2023-12-03 23:03:21 26 4
gpt4 key购买 nike

我有一个图像网格。点击的每个图像都应该在 NavigationView 上推送一个带有图像详细信息的 View 。
导航链接按预期工作,但是当我按下后退按钮时,它会打开下一张图像,依此类推,直到它循环了所有图像。到底是怎么回事?
这是View :

struct ImageGrid: View {

@ObservedObject var part: Part
@State private var showingImagePicker = false
@State private var inputImage: UIImage?
var body: some View {
Button("add images"){
self.showingImagePicker = true
}
LazyVGrid(columns: [GridItem(.adaptive(minimum:100))]){
ForEach(part.images){ image in
ZStack {
Image(uiImage: image.thumb)
.resizable()
.scaledToFit()
NavigationLink (
destination: ImageDetail(image:image),
label: {
EmptyView()
}
).buttonStyle(PlainButtonStyle())
}
}
}
.sheet(isPresented: $showingImagePicker, onDismiss: loadImage) {
ImagePicker(image: self.$inputImage)
}

}
// other functions ...
...
}
这是细节 View
struct ImageDetail: View {

@ObservedObject var image: TrainingImage
var body: some View {
VStack {
Image(uiImage: image.content)
.resizable()
.scaledToFit()
}
}
}
编辑:
这是一个隔离行为的独立示例。当网格位于表单部分内时,它似乎停止正常工作。消除导航链接正常工作的表单和部分


import SwiftUI

extension String: Identifiable {
public var id:Int {
self.hashValue
}
}

struct ImageDetail: View {

var image: String
var body: some View {
VStack {
Image(uiImage: UIImage(systemName: image)!)
.resizable()
.scaledToFit()
}
}
}

struct ContentView: View {
@State var images:[String] = ["plus", "minus"]
var body: some View {
NavigationView {
Form {
Section {
LazyVGrid(columns: [GridItem(.adaptive(minimum:100))]){
ForEach(images){ image in
NavigationLink (
destination: ImageDetail(image: image),
label: {
Image(uiImage: UIImage(systemName: image)!)
.resizable()
.scaledToFit()
}
).buttonStyle(PlainButtonStyle())
}
}
}
}
}
}
}

struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}

最佳答案

自动检测行中的链接是表单/列表功能,但您有几个行,所以效果。解决方案是分离单元格 View 并隐藏自动检测的链接。
使用 Xcode 12.0/iOS 14 测试

struct ContentView: View {
@State var images:[String] = ["plus", "minus"]
var body: some View {
NavigationView {
Form {
Section {
LazyVGrid(columns: [GridItem(.adaptive(minimum:100))]){
ForEach(images){
ImageCellView(image: $0)
}
}
}
}
}
}
}

struct ImageCellView: View {
var image: String
@State private var isActive = false
var body: some View {
Image(uiImage: UIImage(systemName: image)!)
.resizable()
.scaledToFit()
.onTapGesture {
self.isActive = true
}
.background(
NavigationLink (
destination: ImageDetail(image: image), isActive: $isActive,
label: {
EmptyView()
}
))
}
}

关于ios - LazyVGrid 中的 NavigationLink 循环后面的所有条目,SwiftUI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64230801/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com