I see page-based navigation is supported for WatchOS, but is there a way to access this functionality with SwiftUI?
Asked
Active
Viewed 1,633 times
3 Answers
8
If you are using the new SwiftUI App Lifecycle and can't access Storyboards (or just need a solution for App Delegate Cycle without using Storyboards), you can use the new Page View style in SwiftUI 2:
WindowGroup {
TabView {
Page1View()
Page2View()
}
.tabViewStyle(PageTabViewStyle())
}

Sharath Sriram
- 160
- 2
- 5
-
1Yes TabView does it, new for WatchOS 7. This allowed me to finally delete the storyboard entirely. Thanks! – Brett Dec 27 '20 at 14:33
-
-
@alamodey seems like there's a limitation of 10 items. I am unable to add more. But for the main page based navigation, you wouldn't want more than 10 tabs would you? :) – Sharath Sriram Mar 22 '21 at 08:16
-
@SharathSriram I used this UI element to create a series of tips to use my Watch app so I had a bit more than 10 but trimmed it down to 10 now. – alamodey Mar 22 '21 at 10:38
5
Yes, this is possible. First, define hosting controllers for each of your pages like so
class Page1HostingController: WKHostingController<Page1View> {
override var body: Page1View {
self.setTitle("page1")
return Page1View()
}
}
class Page2HostingController: WKHostingController<Page2View> {
override var body: Page2View {
self.setTitle("page2")
return Page2View()
}
}
where Page1View
and Page2View
are your SwiftUI implementations.
Then, add new view controllers to your Interface.storyboard
, set their implementation classes to your new controller classes and connect them via a "next page" segue.

Gereon
- 17,258
- 4
- 42
- 73
-
5That worked, thanks! I thought SwiftUI was supposed to do away with the story boards though. – Brett Jan 16 '20 at 03:15
-1
There is NavigationLink button which triggers a navigation presentation when pressed.
var body: some View {
VStack() {
NavigationLink(destination: Text("Destination view"), label: {
Text("Title of the NavigationLink button")
})
}
}

Viktor Malyi
- 2,298
- 2
- 23
- 40