Reputation: 682
I'm having problems doing navigation in a SwiftUI Apple Watch app. There's minimal resources online regarding this.
I mostly understand how to create SwiftUI views, but I'm not sure what the best way to navigate from SwiftUI view to another SwiftUI view is.
Is there a way to push a user to another SwiftUI screen when they tap a button?
Upvotes: 8
Views: 6735
Reputation: 82
Just replace NavigationView by List, and it works like a charm
struct ContentView : View{
var body : some View {
List{
VStack{
NavigationLink(destination: BPView()) {
Text("Some text")
.foregroundColor(Color.blue);
}
NavigationLink(destination:myView()) {
Text("Show text here")
.foregroundColor(Color.blue);
}
}}
}
}
Upvotes: 0
Reputation: 5320
To provide a very basic example how it can work on watchOS.
struct ListView: View {
var body: some View {
List{
RowView(title: "Row 1")
RowView(title: "Row 2")
RowView(title: "Row 3")
RowView(title: "Row 4")
}
.listStyle(.carousel)
}
}
struct RowView: View {
let title: String
var body: some View {
NavigationLink(destination: Text("Detail \(title)")) {
Text(title)
.frame(height: 80, alignment: .topTrailing)
.listRowBackground(
Color.blue
.cornerRadius(12)
)
}
}
}
Upvotes: 5
Reputation: 264
You can do this by initializing a NavigationLink with your destination view. Its documentation is minimal at the moment, but the tutorial Building Lists and Navigation and WWDC session SwiftUI on watchOS may be of help.
Upvotes: 2