5

Is there a way to use TabView using SwiftUI on WatchOS?

I've seen most of the WWDC talks and they don't mention it for WatchOS. Moreover, in the apple documentation it is not enumerated under SDK's.

Whenever I try to add it to a WatchOS project I get the following error:

'TabView' is unavailable in watchOS

Or is there a good way to replicate the desired TabView ?

I want something similar to this: enter image description here Two different pages that are changed simply by swiping horizontally.

39fredy
  • 1,923
  • 2
  • 21
  • 40

2 Answers2

5

If you want a page based navigation (swipe left/right between view controllers) in your app you simply add another WKHostingController to your project's storyboard. Connect the two hosting controllers by creating a segue between them. The segue is what determines how you navigate between controllers.

This page has a good guide on how to do it: https://www.techotopia.com/index.php/A_WatchKit_Page-based_Navigation_Tutorial

Just keep in mind that you want to add another hosting controller to the storyboard, not anything else.

I have not found a way to do this programmatically in SwiftUI, this seems like the only way to do it for now.

heinz_dieter
  • 315
  • 1
  • 4
  • 12
  • 1
    Thanks for the resource. I have a few issues though. [1] When I added the the new WKHostingController I had to assign it a new `HostingController2.swift` file and inside of that a new View that will be initialized to that `WKHostingController`. This means that the main entry point controller has its own hosting controller which then has its own view (Content view). The same goes for the second `WKHostingController ` The thing is that I'm using an Observable object to be my 'source of truth.' How can I use the same Observable object (EnvironmentObject) in both `Content()` and `Content2()` ? – 39fredy Oct 19 '19 at 06:12
  • 1
    The [2] issue is that initially I want to start with one view. Then lets say click a button and now have 3 `WKHostingController`, any possible way to do that ? – 39fredy Oct 19 '19 at 06:14
  • 1
    I was able to solve the first problem implementing something like [this](https://github.com/onmyway133/blog/issues/467). I'm having issues with the 2nd issue I have 3 Hosting Controllers ( HC1, HC2, HC3) HC1 is the initial controller and it has a `NavigationLink` to HC2 using `NavigationLink(destinationName: "HC2")` In the `storyboard` I have that HC2 has "Relationship 'next page' to HC3" (segue) The issue is that the NavLink does take us to HC2 but from there I cannot reach HC3. Meaning that I do not see the dot and am not able to swipe to the next page (HC3). Any ideas of what's happening ? – 39fredy Oct 19 '19 at 16:24
  • If you use WKHostingController's `.presentController(withNameAndContexts:)` or `reloadRootControllers(withNamesAndContexts:)` you can load a new WKHostingController set that will be shown as pages – Lou Franco Mar 22 '20 at 18:39
5

It's now available starting with watchOS 7: https://developer.apple.com/documentation/swiftui/tabview.

Curiosity
  • 544
  • 1
  • 15
  • 29