9

I created an Apple Watch app using SwiftIU. The main view of the app is a List, with NavigationLink to another List. When I'm in one of the internal Lists, the back button isn't on the top of the view. But when I swipe from the side of the screen (like on iOS), I go back to the main view.

Is there a way to add back the back button to the top of the screen? Is the new gesture the new "back button" for watchOS 7?

Xcode 12.2 (beta 3). Apple Watch S6 (simulator, 7.1) and Apple Watch S5 (real, 7.0). SwiftIU 2.0

The main view:

NavigationView {
    List {
        ForEach(listsModel.lists) { (list) in
            NavigationLink(destination: ListView(list: list)
                            .environmentObject(list.rowsModel)) {
                ListsRowView(list: list)
            }
        }
    }
}
.onAppear {
    listsModel.update()
}
.navigationTitle("ListsView.NavigationTitle")

The internal view:

List(rowsModel.rows) { (row) in
    RowView(row: row, shouldUpdate: $shouldUpdate)
}
.navigationBarHidden(false)
.onAppear {
    instantiateTimer()
    rowsModel.update()
}
.onDisappear {
    cancelTimer()
}
.onReceive(timer) { _ in
    if shouldUpdate {
        rowsModel.update()
    }
}
.navigationTitle(list.name)

Update: In the Canvas simulator, there is a back button, but on the regular simulator, it's not. Canvas:

Canvas

Regular:

Regular

Shahar Melamed
  • 1,534
  • 5
  • 15
  • 23

1 Answers1

20

Remove the NavigationView, so your code would be:

List {
   ForEach(listsModel.lists) { (list) in
        NavigationLink(destination: ListView(list: list)
              .environmentObject(list.rowsModel)) {
        ListsRowView(list: list)
        }
    }
}
.onAppear {
    listsModel.update()
}
.navigationTitle("ListsView.NavigationTitle")

The reason for this is that watchOS always has system NavigationView added by default

Phil Dukhov
  • 67,741
  • 15
  • 184
  • 220
Boom PT
  • 374
  • 4
  • 11
  • 2
    thank you for this! I was struggling to find why the navigation was broken, and removing the NavigationView from all the child views completely fixed it! :) – Lukas Petr Apr 14 '21 at 13:27
  • 1
    Remove Navigation View to actually get navigation view working... that's crazy – Nik May 20 '21 at 12:41
  • Thats genius... who would have thought. Good question and good answer thank you – candyline Dec 02 '21 at 08:37
  • Spent a while wondering why the default back chevron button disappeared on the pushed view. Removed the NavigationView in the root view and voila. – Edison Apr 20 '22 at 07:42