SwiftUI Navigation

Jun 20 2024 · Swift 5.9, iOS 17.0, Xcode 15.0

Lesson 03: TabView in SwiftUI

Demo

Episode complete

Play next episode

Next

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

Setting Tab Badges

SwiftUI also lets you add a badge to each tab. This badge provides a way to share extra information with the user, but the available space limits the amount of data you can show. You’ll add a badge item to indicate the number of incoming and outgoing flights to the Flight Status and a short text badge showing the date to the central tab.

.badge(shownFlights.filter { $0.direction == .arrival }.count)
.badge(shownFlights.filter { $0.direction == .departure }.count)
var shortDateString: String {
  let dateF = DateFormatter()
  dateF.timeStyle = .none
  dateF.dateFormat = "MMM d"
  return dateF.string(from: Date())
}
.badge(shortDateString)

Paging Views

You’ve explored the most common tab view, which shows each tab as a separate space at the bottom of the view. SwiftUI also supports two other styles of tabs based on pages. Each tab takes the entire view in this style, with SwiftUI providing a minimal navigation indicator that floats over the content. You’ll now convert your tabs to this style to better understand their differences.

.tabViewStyle(.page)
.indexViewStyle(.page(backgroundDisplayMode: .always))
.navigationTitle("Arrivals")
.navigationTitle("All Flights")
.navigationTitle("Departures")
See forum comments
Cinema mode Download course materials from Github
Previous: TabView in SwiftUI Next: Conclusion