Fabrizio Duroni

Mar 5, 2020

6 min read

Create a custom TabBar in SwiftUI

SwiftUI has been introduced by Apple during the last WWDC. I started to use it for a personal project to understand its potential. Let’s see how I used SwiftUI to create a custom TabBar with modal and detail navigation.

NB.: Considering the success of this blog post, I decided to create a new custom TabBar SwiftUI library called TabBarUIAction. Go and check it out!!!

In the last week I started to develop a new personal project: a new iOS mobile app (that I will show you in the following months). As a consequence of the fact that this was a greenfield project I decided to use SwiftUI to develop all the User Interface and Combine for data binging. These frameworks are the Apple implementation of reactive programming (combine) and declarative user interface programming (SwiftUI). In this post we will focus on SwiftUI (we will see combine in another post later 😉). Even if it is still not ready for a complete production product because a lot of UIKit part are missing and you have to expose them manually and because some stuff doesn’t work as expected, SwiftUI is a big step forward with respect to the old imperative UIKit approach (sometimes I still have headache about Autolayout issues and constraint definition 😆).
In this post I will show you how I created a custom tab bar with the ability to open a modal from a tab bar item (like some major apps, e.g. Instagram) and a customized navigation where the TabBar is shown only on the first level of navigation. Below you can find a video that shows the final result.

Let’s start by defining and enum that will identify our tabs in a more expressive way. In the custom tab bar there will be three element, but only two are real tabs (as you see from the video above the one in the middle is modal).

The main view MainView contains 2 variable fields:

  • currentView, a @State variable that keeps the current tab selected
  • showModal, a @State variable that is used to manage the presentation of the modal from the central button of the TabBar

The body of the view is composed by a NavigationView that contains a VStack with main components of the custom tab bar implementation:

  • CurrentScreen, that contains and show the current screen selected
  • TabBar, that contains custom tab bar with all its logic

There are also some style customization like forcing the navigationViewStyle to StackNavigationViewStyle, in order to avoid to see a blank screen on iPad (because the NavigationView on iPad is a split view by default 😆). Last but not least, there is a sheet defined to show a ScreenModal view as a modal whenever the state variable showModal changes.

Let’s see first the implementation of the CurrentView. This view is really simple: it shows a specific screen based on the value of its currentView. As you may already noticed, currentView is a not a state variable. It is annotated with the property wrapper @Binding. What does it mean? The @Binding property wrapper lets us declare that one value actually comes from elsewhere, and should be shared in both places. This basically means that this variable will be updated as soon as the variable passed at construction time for this view will be update, and the view will be update consequently by changing the screen shown.

Let’s see the TabBar implementation. Also this view contains two @Binding variables:

  • The first one is again currentView, that will be updated by its children TabBaItem views whenever the user will tap on them.
  • The other one is showModal, that is a boolean modified by the ShowModalTabBarItem with the method toggle(), that invert the current value of the boolean. As you remember we defined a state variable showModal in the MainView. So as soon as the user taps on ShowModalTabBarItem the showModal state is modified and the MainView will show a modal screen ScreenModal.
    Beautiful!! 😍

The implementation of TabBarItem is simple. It contains the styling for the tab bar item. The important thing to notice is here is the onTapGesture modifier. Whenever a user taps on it the @Binding currentView variable will be updated and so also the @State variable currentView in the MainView and consequently also the CurrentScreen view will be update.

The ShowModalTabBarItem is also simple. It contains the style for the custom TabBar item and it attach the action to show the modal to the view with the onTapGestureModifier.

Screen1, Screen2 and ScreenModal are full screen VStack view that defined the content for each screen. The important part is the definition of the title on Screen1 and Screen2 to modify the navigation bar using the navigationBarTitle modifier. In the Screen1 view there's also a NavigationLink that let us navigate to a detail/second level screen. As you remember in the MainView we defined a NavigationView as container of the custom TabBar with the current content. This let us keep the TabBar only on the first level of navigation and it when the user enters in the details screen.

You can find all the code shown in this post in this Github repo. Apple has done a great job with SwiftUI and I hope that the framework will receive in the future updates all the missing UIKit pieces (while still missing a lot of stuff on the web/Safari side). If you already used powerful declarative framework like React or React Native you will feel at home (seems like a copy of each other 😄). Stay tuned for other post about SwiftUI and Combine soon. 💞

Originally published at https://www.fabrizioduroni.it on March 5, 2020.