SwiftUI: Reacting on TabView events

When rebuilding our app, we first tried to work with an existing Tab Bar which we wrapped to use it as View in the new SwiftUI parts of our application.

However, this causes some boilerplate code, so I tried to use SwiftUI’s original `TabView`. While it is pretty easy to set up the view, it turned out that it is not so easy to react on the event of tapping a tab item in order to execute some custom code, such as save the selection and restore it on app restart.

(I’ll just focus on the important stuff, so not all code is shown here required to have the examples work.)

This is working, so I tried to have a `didSet` for the `selectedIndex` property. No way.

This is stupid, as it catches all taps somewhere on the view.

Nice idea, but does not react at all.

This time, it’s working. The TabView is bound to the model’s `selectedIndex` and updates it automatically.

The model’s `@Published var selectedIndex` can be asked for the `didSet` operation, where a custom action can be performed (in this case: save the selected index value to the user defaults).

When the model is initialized, the saved value is used to initialize the `selectedIndex`.

Although the final solution is pretty simple, it is not so obvious, especially for a guy like me who is not yet very used to the reactive style. This article here put me on the right track, so thanks to Andrea for his work!

The next challenge I’m facing is to react on the reordering of the `TabView` tab items. If anyone already has a solution for that, I’d be happy to share it here!

I just noticed that there seems to be a bug in `TabView`: When using it without binding, the “More…” tab bar item is handled correctly, i.e., you can tap on it and it shows the relevant screen.

But: As soon as there is a binding attached to the `TabView`, that “More…” item appears, but does not react on a tap. On a large device (such as an iPad), all tab bar items are shown and reactive.

For me it seems that it is still a valid strategy to wrap a UITabBarController

Helping companies and humans to evolve, and trying to be agile in an own product development.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store