SwiftUI Grid Images Can't be Tapped to Show DetailView

北战南征 提交于 2021-01-29 09:54:02

问题


I'm trying to create a collection view in SwiftUI. I can create the grid (of images in this case) but I have not found a way to click an image and transition to another view - neither a modal nor a full view. I am storing the images in CoreData and am using a method from Paul Hudson to break the images into rows:

https://www.hackingwithswift.com/example-code/language/how-to-split-an-array-into-chunks

This code does allow transitions, but with a single click it cycles through each of the images in the HStack row even though each photo has its own NavigationLink wrapper.

I have also tried a version where I made the grid from buttons and put the images into the buttons. That was worse - the detail content was not correct and Xcode complained mightily that: "Warning: Attempt to present <> on <> which is already presenting (null)." I also tried adding .onTapGesture{} to each image. Similar failure as the buttons.

struct ChunkedContentView: View {

    @Environment(\.managedObjectContext) var managedObjectContext
    @FetchRequest(fetchRequest: DermPhoto.getAllDermPhotos()) var dermPhotos: FetchedResults<DermPhoto>

    //@State private var columnCount = 3//hard coded to 3 at the moment
    @State private var showDetailView = false

    var body: some View {

        let chunkThis = DermPhoto.chunkTheDermPhotos()

        return GeometryReader { geo in
            NavigationView {
                List {
                    ForEach(0 ..< chunkThis.count) { index in
                        HStack {
                            ForEach(chunkThis[index]) { p in
                                NavigationLink(destination: DetailView(passedDerm: p)) {

                                Image(uiImage: UIImage(data: p.myImage!)!)
                                    .resizable()
                                    .aspectRatio(contentMode: .fill)
                                    .frame(width: ((geo.size.width / 3) - 15), height: ((geo.size.width / 3) - 15))
                                    .border(Color.black)
                                    .clipped()
                                    //comment out NavigationLink and this does not work either
                                    //.onTapGesture {
                                    //self.showDetailView.toggle()
                                    //}
                                    //.sheet(isPresented: self.$showDetailView) {
                                    //DetailView(passedDerm: p)
                                    //}
                                    //either above or nav link
                                }//nav link
                            }
                        }
                    }
                }
                .navigationBarTitle("Time Series")
            }//nav
        }//geo
    }
}

As a second question, is there a way to disable showing the disclosure indicator of a NavigationLink?

Any guidance would be appreciated. Xcode 11.3 (11C29)


回答1:


@Asperi deserves the credit - use ScrollView instead of List. That also removes the disclosure indicator. If you use images you need to add .buttonStyle(PlainButtonStyle()) to the NavigationLink. If you use system icons you may not need that step.



来源:https://stackoverflow.com/questions/59440226/swiftui-grid-images-cant-be-tapped-to-show-detailview

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!