hidden(_:)
Hides the tab from the user.
Declaration
nonisolated func hidden(_ hidden: Bool = true) -> some TabContent<Self.TabValue>
Parameters
- hidden:
Whether the tab is hidden.
Discussion
Unlike removing or adding tabs with if statements, this modifier preserves the associated state of the tab’s content view when the tab is hidden.
When you hide a tab using this modifier, make sure to update the TabView’s selection appropriately, since that tab could be selected.
The following example shows a TabView with 4 tabs in compact and 5 tabs in regular. In compact, one of the tabs is a ‘Browse’ tab that displays a custom list view. This list view allows navigating to the destinations that are contained within the ‘Library’ and ‘Playlists’ sections in regular. The navigation path and the selection state are updated when the number of tabs changes.
struct BrowseTabExample: View {
@Environment(\.horizontalSizeClass) var sizeClass
@State var selection: MusicTab = .listenNow
@State var browseTabPath: [MusicTab] = []
@State var playlists = [Playlist("All Playlists"), Playlist("Running")]
var body: some View {
TabView(selection: $selection) {
Tab("Listen Now", systemImage: "play.circle", value: .listenNow) {
ListenNowView()
}
Tab("Radio", systemImage: "dot.radiowaves.left.and.right", value: .radio) {
RadioView()
}
Tab("Search", systemImage: "magnifyingglass", value: .search) {
SearchDetailView()
}
Tab("Browse", systemImage: "list.bullet", value: .browse) {
LibraryView(path: $browseTabPath)
}
.hidden(sizeClass != .compact)
TabSection("Library") {
Tab("Recently Added", systemImage: "clock", value: MusicTab.library(.recentlyAdded)) {
RecentlyAddedView()
}
Tab("Artists", systemImage: "music.mic", value: MusicTab.library(.artists)) {
ArtistsView()
}
}
.hidden(sizeClass == .compact)
TabSection("Playlists") {
ForEach(playlists) { playlist in
Tab(playlist.name, image: playlist.image, value: MusicTab.playlists(playlist)) {
playlist.detailView()
}
}
}
.hidden(sizeClass == .compact)
}
.tabViewStyle(.sidebarAdaptable)
.onChange(of: sizeClass, initial: true) { _, sizeClass in
if sizeClass == .compact && selection.showInBrowseTab {
browseTabPath = [selection]
selection = .browse
} else if sizeClass == .regular && selection == .browse {
selection = browseTabPath.last ?? .library(.recentlyAdded)
}
}
}
}
}
struct LibraryView: View {
@Binding var path: [MusicTab]
var body: some View {
NavigationStack(path: $path) {
List {
ForEach(MusicLibraryTab.allCases, id: \.self) { tab in
NavigationLink(tab.rawValue, value: MusicTab.library(tab))
}
// Code to add playlists here
}
.navigationDestination(for: MusicTab.self) { tab in
tab.detail()
}
}
}
}