Reputation: 2890
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
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
Reputation: 36304
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
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