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.

5
  • 2
    It seems like you found your answer. TabView is not available on the watch. Commented Sep 21, 2019 at 2:49
  • Amazing answer! Commented Sep 21, 2019 at 2:51
  • Why do you want a TabView in WatchOS? Commented Sep 21, 2019 at 3:02
  • I want to create a UI similar to this Commented Sep 21, 2019 at 3:10
  • You can write it yourself. It is just an HStack containing 3 Buttons. Each button contains a VStack with an Image and Label inside Commented Sep 21, 2019 at 7:33

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

4 Comments

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() ?
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 ?
I was able to solve the first problem implementing something like this. 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 ?
If you use WKHostingController's .presentController(withNameAndContexts:) or reloadRootControllers(withNamesAndContexts:) you can load a new WKHostingController set that will be shown as pages
5

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.