How to do navigation in SwiftUI in watchOS

Issue #447

NavigationView is not available on WatchKit, but we can just use NavigationLink

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
List(services.map({ AnyService($0) })) { anyService in
NavigationLink(destination:
ItemsView(service: anyService.service)
.navigationBarTitle(anyService.service.name)
.onDisappear(perform: {
anyService.service.requestCancellable?.cancel()
})
) {
HStack {
Image(anyService.service.name)
.resizable()
.frame(width: 30, height: 30, alignment: .leading)
Text(anyService.service.name)
}
}
}

Adding NavigationLink to a View adds a round shadow cropping effect, which is usually not want we want.

But we shouldn’t wrap Button as Button handles its own touch event, plus it has double shadow effect.

1
2
3
4
5
6
7
NavigationLink(destination:
QRCodeView(title: item.title, url: item.url)
) {
Button(action: {}) {
Text("Open")
}
}

Just use Text and it’s good to go

1
2
3
4
5
NavigationLink(destination:
QRCodeView(title: item.title, url: item.url)
) {
Text("Open")
}

Comments