Cannot use TabView on SwiftUI, WatchOS
Asked Answered
S

2

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.

Statist answered 21/9, 2019 at 2:26 Comment(5)
It seems like you found your answer. TabView is not available on the watch.Glow
Amazing answer!Statist
Why do you want a TabView in WatchOS?Carty
I want to create a UI similar to thisStatist
You can write it yourself. It is just an HStack containing 3 Buttons. Each button contains a VStack with an Image and Label insideTad
T
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.

Tabathatabb answered 19/10, 2019 at 0:22 Comment(4)
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() ?Statist
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 ?Statist
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 ?Statist
If you use WKHostingController's .presentController(withNameAndContexts:) or reloadRootControllers(withNamesAndContexts:) you can load a new WKHostingController set that will be shown as pagesHipster
P
5

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

Phillip answered 22/6, 2020 at 20:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.