Bird
Raised Fist0
iOS Swiftmobile~20 mins

TabView for tab navigation in iOS Swift - Mini App: Build & Ship

Choose your learning style10 modes available
Build: MainTabView
This screen shows a tab bar at the bottom with three tabs: Home, Search, and Profile. Each tab displays a simple label with the tab name.
Target UI
┌─────────────────────────────┐
│          MainTabView        │
├─────────────────────────────┤
│                             │
│          [Home Tab]          │
│                             │
│         "Home Screen"       │
│                             │
├─────────────────────────────┤
│ Home | Search | Profile     │
└─────────────────────────────┘
Use UITabBarController to create tab navigation
Add three tabs labeled Home, Search, and Profile
Each tab shows a centered label with the tab name
Use system icons for each tab: house, magnifyingglass, person
Tabs should be selectable and switch content accordingly
Starter Code
iOS Swift
import UIKit

class MainTabViewController: UITabBarController {
    override func viewDidLoad() {
        super.viewDidLoad()
        // TODO: Setup the tab bar with three tabs
    }
}

// TODO: Create simple UIViewController subclasses for Home, Search, and Profile screens
Task 1
Task 2
Task 3
Task 4
Task 5
Solution
iOS Swift
import UIKit

class HomeViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .systemBackground
        let label = UILabel()
        label.text = "Home Screen"
        label.font = .systemFont(ofSize: 24, weight: .medium)
        label.textAlignment = .center
        label.translatesAutoresizingMaskIntoConstraints = false
        view.addSubview(label)
        NSLayoutConstraint.activate([
            label.centerXAnchor.constraint(equalTo: view.centerXAnchor),
            label.centerYAnchor.constraint(equalTo: view.centerYAnchor)
        ])
    }
}

class SearchViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .systemBackground
        let label = UILabel()
        label.text = "Search Screen"
        label.font = .systemFont(ofSize: 24, weight: .medium)
        label.textAlignment = .center
        label.translatesAutoresizingMaskIntoConstraints = false
        view.addSubview(label)
        NSLayoutConstraint.activate([
            label.centerXAnchor.constraint(equalTo: view.centerXAnchor),
            label.centerYAnchor.constraint(equalTo: view.centerYAnchor)
        ])
    }
}

class ProfileViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .systemBackground
        let label = UILabel()
        label.text = "Profile Screen"
        label.font = .systemFont(ofSize: 24, weight: .medium)
        label.textAlignment = .center
        label.translatesAutoresizingMaskIntoConstraints = false
        view.addSubview(label)
        NSLayoutConstraint.activate([
            label.centerXAnchor.constraint(equalTo: view.centerXAnchor),
            label.centerYAnchor.constraint(equalTo: view.centerYAnchor)
        ])
    }
}

class MainTabViewController: UITabBarController {
    override func viewDidLoad() {
        super.viewDidLoad()

        let homeVC = HomeViewController()
        homeVC.tabBarItem = UITabBarItem(title: "Home", image: UIImage(systemName: "house"), tag: 0)

        let searchVC = SearchViewController()
        searchVC.tabBarItem = UITabBarItem(title: "Search", image: UIImage(systemName: "magnifyingglass"), tag: 1)
        searchVC.tabBarItem.badgeValue = "3"

        let profileVC = ProfileViewController()
        profileVC.tabBarItem = UITabBarItem(title: "Profile", image: UIImage(systemName: "person"), tag: 2)

        viewControllers = [homeVC, searchVC, profileVC]
    }
}

We created three simple view controllers for Home, Search, and Profile. Each shows a centered label with the tab name. In MainTabViewController, we set up the tab bar by assigning these controllers as tabs. We used system icons for each tab bar item for clarity and consistency. This setup allows users to tap the tabs at the bottom to switch between screens easily.

Final Result
Completed Screen
┌─────────────────────────────┐
│          MainTabView        │
├─────────────────────────────┤
│                             │
│          Home Screen         │
│                             │
├─────────────────────────────┤
│ 🏠 Home | 🔍 Search | 👤 Profile │
└─────────────────────────────┘
Tap 'Search' tab to show 'Search Screen' label
Tap 'Profile' tab to show 'Profile Screen' label
Tap 'Home' tab to return to 'Home Screen'
Stretch Goal
Add badge notifications to the Search tab showing number 3
💡 Hint
Set the tabBarItem.badgeValue property of the SearchViewController to "3"

Practice

(1/5)
1. What is the main purpose of using TabView in a SwiftUI app?
easy
A. To create a tab bar for switching between different views
B. To display a list of items in a scrollable view
C. To handle user input forms
D. To show a single full-screen view without navigation

Solution

  1. Step 1: Understand the role of TabView

    TabView is designed to create a tab bar interface that lets users switch between multiple views easily.
  2. Step 2: Compare with other UI components

    Other options like lists or forms serve different purposes, not tab navigation.
  3. Final Answer:

    To create a tab bar for switching between different views -> Option A
  4. Quick Check:

    TabView = Tab bar navigation [OK]
Hint: TabView always creates tabs for multiple views [OK]
Common Mistakes:
  • Confusing TabView with List or NavigationView
  • Thinking TabView shows only one view
  • Assuming TabView handles forms or inputs
2. Which of the following is the correct syntax to add a tab item with a label and system image inside a TabView in SwiftUI?
easy
A. Text("Home").tabItem(Label("Home", systemImage: "house"))
B. Text("Home").tabItem(Label("Home", image: "house"))
C. Text("Home").tabItem { Label("Home", image: "house") }
D. Text("Home").tabItem { Label("Home", systemImage: "house") }

Solution

  1. Step 1: Recall the correct Label initializer

    The Label view uses the initializer Label("text", systemImage: "iconName") to show text with a system icon.
  2. Step 2: Check tabItem syntax

    The tabItem modifier takes a trailing closure with the Label view inside braces {}.
  3. Final Answer:

    Text("Home").tabItem { Label("Home", systemImage: "house") } -> Option D
  4. Quick Check:

    tabItem uses Label with systemImage inside braces [OK]
Hint: Use Label("text", systemImage: "icon") inside tabItem braces [OK]
Common Mistakes:
  • Using image: instead of systemImage:
  • Missing braces {} around tabItem content
  • Passing tabItem parameters incorrectly
3. Given the code below, what will be displayed when the app runs?
TabView {
  Text("First Tab").tabItem { Label("First", systemImage: "1.circle") }
  Text("Second Tab").tabItem { Label("Second", systemImage: "2.circle") }
}
medium
A. Only the text 'First Tab' is shown without any tabs
B. A tab bar with two tabs labeled 'First' and 'Second', showing respective texts when selected
C. A runtime error because TabView needs a NavigationView
D. A tab bar with icons but no labels

Solution

  1. Step 1: Analyze the TabView structure

    There are two tabs, each with a Text view and a tabItem with label and icon.
  2. Step 2: Understand TabView behavior

    TabView shows a tab bar with the labels and icons, switching the displayed Text accordingly.
  3. Final Answer:

    A tab bar with two tabs labeled 'First' and 'Second', showing respective texts when selected -> Option B
  4. Quick Check:

    TabView shows tabs with labels and content [OK]
Hint: TabView shows tabs with label and content views [OK]
Common Mistakes:
  • Thinking TabView requires NavigationView
  • Expecting only one tab to show
  • Assuming labels won't appear
4. Identify the error in this SwiftUI TabView code:
TabView {
  Text("Home")
    .tabItem(Label("Home", systemImage: "house"))
  Text("Settings")
    .tabItem { Label("Settings", systemImage: "gear") }
}
medium
A. The first tabItem is missing braces {} around Label
B. The second tabItem uses incorrect systemImage name
C. TabView cannot have more than one tabItem
D. Text views cannot be used inside TabView

Solution

  1. Step 1: Check tabItem syntax for each tab

    The first tabItem uses parentheses without braces, which is incorrect; it needs braces {} around the Label.
  2. Step 2: Verify the second tabItem syntax

    The second tabItem correctly uses braces with Label inside.
  3. Final Answer:

    The first tabItem is missing braces {} around Label -> Option A
  4. Quick Check:

    tabItem requires braces around content [OK]
Hint: Always use braces {} for tabItem content [OK]
Common Mistakes:
  • Using parentheses instead of braces for tabItem
  • Assuming systemImage names cause errors
  • Thinking TabView limits number of tabs
5. You want to create a TabView with three tabs: Home, Search, and Profile. Each tab should show a different view and have a label with a system icon. Which code snippet correctly implements this with minimal repetition?
hard
A. TabView { HomeView().tabItem { Text("Home") } SearchView().tabItem { Text("Search") } ProfileView().tabItem { Text("Profile") } }
B. TabView { HomeView().tabItem(Label("Home", systemImage: "house")) SearchView().tabItem(Label("Search", systemImage: "magnifyingglass")) ProfileView().tabItem(Label("Profile", systemImage: "person")) }
C. TabView { HomeView().tabItem { Label("Home", systemImage: "house") } SearchView().tabItem { Label("Search", systemImage: "magnifyingglass") } ProfileView().tabItem { Label("Profile", systemImage: "person") } }
D. TabView { HomeView().tabItem { Label("Home") } SearchView().tabItem { Label("Search") } ProfileView().tabItem { Label("Profile") } }

Solution

  1. Step 1: Check tabItem syntax for each tab

    TabView { HomeView().tabItem { Label("Home", systemImage: "house") } SearchView().tabItem { Label("Search", systemImage: "magnifyingglass") } ProfileView().tabItem { Label("Profile", systemImage: "person") } } correctly uses braces with Label including both text and systemImage for each tab.
  2. Step 2: Review other options for errors

    TabView { HomeView().tabItem(Label("Home", systemImage: "house")) SearchView().tabItem(Label("Search", systemImage: "magnifyingglass")) ProfileView().tabItem(Label("Profile", systemImage: "person")) } misses braces, causing syntax errors. TabView { HomeView().tabItem { Text("Home") } SearchView().tabItem { Text("Search") } ProfileView().tabItem { Text("Profile") } } uses Text instead of Label, so no icons. TabView { HomeView().tabItem { Label("Home") } SearchView().tabItem { Label("Search") } ProfileView().tabItem { Label("Profile") } } uses Label without systemImage, so no icons.
  3. Final Answer:

    TabView { HomeView().tabItem { Label("Home", systemImage: "house") } SearchView().tabItem { Label("Search", systemImage: "magnifyingglass") } ProfileView().tabItem { Label("Profile", systemImage: "person") } } -> Option C
  4. Quick Check:

    Use Label with text and systemImage inside braces for tabItem [OK]
Hint: Use Label with text and systemImage inside braces for tabs [OK]
Common Mistakes:
  • Omitting braces {} around tabItem content
  • Using Text instead of Label for icons
  • Forgetting systemImage parameter
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard