photosynthesis
photosynthesis

Reputation: 2890

@State variable in onTapGesture not getting updated

with the code below I was expecting when the image in VStack was tapped, it shows another image in the full screen cover but the imageName variable does not seem to get set to jugg as in the new full screen it has only a gray background

struct TestView: View {
    @State var imageName = ""
    @State var showFullscreen = false

    var body: some View {
        VStack {
            Image("drow")
                .resizable()
                .scaledToFit()
                .frame(width: 100)
                .onTapGesture {
                    self.imageName = "jugg"
                    self.showFullscreen = true
            }
        }
        .fullScreenCover(isPresented: $showFullscreen) {
            ZStack {
                Color.gray.ignoresSafeArea()
                Image(imageName)
                    .resizable()
                    .scaledToFit()
                    .frame(width: 380)
            }
        }
    }
}

Upvotes: 1

Views: 806

Answers (3)

tengiz kiknadze
tengiz kiknadze

Reputation: 1

I had similar issue , i was passing @Binding variable to ChildView and it was always failing on first TapGesture . instead , i created Published variable in viewModel and used it in both Views .

Upvotes: 0

as mentioned in the comments, use the .fullScreenCover(item: ..) version of the fullScreenCover, such as:

struct ImageName: Identifiable {
    let id = UUID()
    var name = ""
}

struct TestView: View {
    @State var imageName: ImageName?
    
    var body: some View {
        VStack {
            Image("drow").resizable().scaledToFit().frame(width: 100)
                .onTapGesture {
                    imageName = ImageName(name: "drow")
                }
        }
        .fullScreenCover(item: $imageName) { img in
            ZStack {
                Color.gray.ignoresSafeArea()
                Image(img.name).resizable().scaledToFit().frame(width: 380)
            }
        }
    }
}

Upvotes: 1

Jia Chen
Jia Chen

Reputation: 73

Seems to only work if you create a separate SwiftUI view and pass in the imageName as a @Binding variable

struct TestView: View {
    @State var imageName = ""
    @State var showFullscreen = false
    
    var body: some View {
        VStack {
            Image("drow")
                .resizable()
                .scaledToFit()
                .frame(width: 100)
                .onTapGesture {
                    
                    imageName = "jugg"
                    
                    showFullscreen = true
                }
        }
        .fullScreenCover(isPresented: $showFullscreen) {
            CoverView(imageName: $imageName)
        }
    }
}
struct CoverView: View {
    
    @Binding var imageName: String
    
    var body: some View {
        ZStack {
            Color.gray.ignoresSafeArea()
            Image(imageName)
                .resizable()
                .scaledToFit()
                .frame(width: 380)
        }
    }
}

Upvotes: 0

Related Questions