I’m trying to make both the text and the image clickable so that it opens a new view. The text is clickable, but NOT the image. How do I resolve this??
Button("Info") {
isSheetPresented.toggle()
}
.font(.custom("airstrike", size: 30))
.foregroundStyle(Color.black)
.padding(.horizontal)
.drawingGroup() // prevent end clipping of custom font
Image(systemName: "info.circle")
.foregroundStyle(Color.blue)
.sheet(isPresented: $isSheetPresented) {
SetupView()
}
By default the label: closure places the items side by side (as in an implied HStack) so it you want the SF symbol and the Text to be one above the other then wrap them in a VStack.
Most developers add the .sheet modifier to the top View inside the body property which makes the code a little cleaner and easier to read. Adding the .sheet modifier to the Image view adds clutter. I didn’t include the .sheet simply because it was the button label: that was the objective to get both the Text and the Image to respond to a tap gesture.
This is the complete View struct containing the example.
.drawingGroup() is not required since there isn’t any complex rendering being performed.
See this article from Paul Hudson where .drawingGroup does make a difference: