April 24th, 2024 — Tiffany Pan
Welcome to the last lecture of this course! Today, we’ll be covering how to implement a very useful concept for projects and especially for your hack challenge: tab views!
These are implemented using a UITabBarController class in UIKit, which is what this lecture will cover.
An UITabBarController
is most commonly used to create the tab switching functionality when developing in UIKit.
@MainActor
class UITabBarController : UIViewController {}
<aside>
<img src="/icons/thought-dialogue_orange.svg" alt="/icons/thought-dialogue_orange.svg" width="40px" /> @MainActor
simply indicates and enforces that this View Controller performs its actions on the main thread. This means that any UI updates made to our tab view controller (tapping on a tab) will always be updated on the main thread. See more about concurrency in Concurrency.
</aside>
At its core, an UITabBarController
is simply a container view controller that allows the user to select between designated child view controllers to display. The selection is done via tab icons that we all know and love, namely something like
Spotify tabs. Users can easily select between the app's 3 main functionalities.
Developers generally tend to use tab views for myriad of things, such as displaying a bunch of different functionalities and presenting information in different ways. In any case, the important thing to note is that an UITabBarController allows you to manage completely different interfaces in each tab - i.e. each tab can hold completely different views.
A TabBarController
is powerful because it handles the tab switching for you - you only need to specify the specific root views for each tab by instatiating an UITabBarItem
and it handles the rest of the logic, including updating the UI when the user taps on a tab. Let's dive more into how we can set one up.