I have a NavigationView with many NavigationLinks in SwiftUI for Mac.
Whenever I press a Navigation Item, the Navigation Detail is being displayed on the right. However, I have a custom styling for my active Navigation Items. When I press a Item, I want to call an action. I have tried onTapGesture() function on the NavigationLink, however it is not working correctly/ as expected.
Here is my code:
NavigationView {
VStack{
NavigationLink(destination: SecondContentView()) {
VStack
{
Image("Calendar")
.resizable().frame(width:40, height: 40)
.colorMultiply(currentSelected == 0 ? Color(red: 57 / 255, green: 214 / 255, blue: 155 / 255) : Color(red: 255 / 255, green: 255 / 255, blue: 255 / 255))
Text("Dates")
.foregroundColor(currentSelected == 0 ? Color(red: 57 / 255, green: 214 / 255, blue: 155 / 255) : Color(red: 255 / 255, green: 255 / 255, blue: 255 / 255))
}
}
.buttonStyle(PlainButtonStyle())
.padding(18)
.background(currentSelected == 0 ? Color(.controlBackgroundColor) : Color(.clear))
.zIndex(50)
.onTapGesture {
NSLog("Tapped Nav")
self.currentSelected = 0
}
The log "Tapped Nav" is only output sometimes.. I think there is a problem with the image and text which is inside a navigation item. Is there a better way to call an action function, when I click on the item?
