Created
January 26, 2026 10:49
-
-
Save simonbs/a39a8536f9ba1a4b68903147fed02737 to your computer and use it in GitHub Desktop.
How can I present DetailView with a zoom transition without clipping the navigation bar of the presenting view?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import Foundation | |
| import SwiftUI | |
| struct ColorItem: Hashable, Identifiable { | |
| let id = UUID() | |
| let color: Color | |
| } | |
| extension ColorItem { | |
| static var allItems: [Self] { | |
| let colors: [Color] = [.red, .orange, .yellow, .green, .cyan, .blue, .indigo, .purple] | |
| return (0 ..< 50).map { Self(color: colors[$0 % colors.count]) } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import SwiftUI | |
| struct ContentView: View { | |
| @Namespace private var namespace | |
| @State private var selectedItem: ColorItem? | |
| private let items = ColorItem.allItems | |
| private let columns: [GridItem] = Array(repeating: GridItem(.flexible(), spacing: 12), count: 3) | |
| var body: some View { | |
| NavigationStack { | |
| ScrollView { | |
| LazyVGrid(columns: columns, spacing: 12) { | |
| ForEach(items, id: \.self) { item in | |
| Button { | |
| selectedItem = item | |
| } label: { | |
| RoundedRectangle(cornerRadius: 14) | |
| .fill(item.color) | |
| .aspectRatio(1, contentMode: .fit) | |
| .matchedTransitionSource(id: item.id, in: namespace) | |
| } | |
| } | |
| } | |
| .padding(12) | |
| } | |
| .navigationBarTitleDisplayMode(.inline) | |
| .navigationTitle("Example") | |
| .fullScreenCover(item: $selectedItem) { item in | |
| DetailView(item: item) | |
| .navigationTransition(.zoom(sourceID: item.id, in: namespace)) | |
| } | |
| } | |
| } | |
| } | |
| #Preview { | |
| ContentView() | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import SwiftUI | |
| struct DetailView: View { | |
| let item: ColorItem | |
| @Environment(\.dismiss) private var dismiss | |
| var body: some View { | |
| NavigationStack { | |
| VStack { | |
| RoundedRectangle(cornerRadius: 14) | |
| .fill(item.color) | |
| .aspectRatio(1, contentMode: .fit) | |
| } | |
| .padding() | |
| .navigationBarTitleDisplayMode(.inline) | |
| .navigationTitle("Item") | |
| .toolbar { | |
| ToolbarItem(placement: .topBarLeading) { | |
| Button { | |
| dismiss() | |
| } label: { | |
| Label("Back", systemImage: "chevron.left") | |
| .labelStyle(.iconOnly) | |
| } | |
| } | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment