Note 19-mar-2020: In the meantime, I went much deeper into the question how a scrollable tab view with animations can be realized using pure SwiftUI. I came up with a solution that is published on GitHub.
While the solution described in my previous article is working pretty well for the displayed tabs, it has a problem when the “More…” tab is used and a view controller from there is selected. In this case, the
didSelect delegate function will not be called.
I’ll describe here a modification to the above solution that is also able to handle that situation.
ViewControllers instead of TabItems
First, we modify the
IRTabBar so that it gets the required view controllers directly, instead of providing the tab bar items:
- This is what we had, we’re replacing that with
- this declaration.
As a consequence, we have to modify our
makeUIViewController function so that it just takes the new property value, instead of creating the list of view controllers:
Next, we have to change the
didSelect delegate function in our
Here, we did two things:
- First, we have to get the index for given view controller, in order to set the
- Second, we have to check if the “More…” tab was selected. In this case, UIKit will deliver us a
UITableViewController. We need to react on a table view event, so we set a delegate if necessary. this requires another property for the
Now, we have to add the handling of the table view events:
Because we do not know how many tab bar items are really shown on the device, we have to find it our indirectly but just taking the overall count of the available view controllers, and the number of entries just shown in the table view. The difference is to be added to the index path’s
row count in order to get the
Adopting model and view
Obviously, we have to slighly modify the model and view in order to not provide items, but view controllers.
We replace the previous
items property (1) with a lazy one for the view controllers (2).
With these modifications, you are able to react even on events of the “More…” action, in a SwiftUI setup.
I provided the sources on Github.