Of course. This is a classic SwiftUI layout issue. Let's break down why it's happening and how to fix it.
The "Why": Root Cause
The problem is caused by this line:
swift
.blur(radius: isShowingDetail ? 2 : 0)
You are applying the .blur() modifier directly to the NavigationView. Here's the sequence of events:
Tap: You tap a list row.
State Change:isShowingDetail becomes true.
View Update: SwiftUI re-evaluates the view body. It sees that the NavigationView must now have a blur(radius: 2).
Layout Shift: To render a blur, the system needs a little extra space around the edges of the view to "bleed" the blurred pixels. This causes a subtle change in the view's calculated frame or rendering bounds.
Re-layout: This change in the view's frame forces a re-layout of its contents (the List). This re-layout is what you perceive as the list "moving" or "jumping" just before your detail view appears.
The "How": The Solution
The solution is to decouple the blurred background from the foreground content. The best way to do this in SwiftUI is with a ZStack. You will place your list in the bottom layer and the detail view in the top layer. This way, blurring the bottom layer doesn't affect the layout of the whole screen.
You should also use a proper navigation pattern, like presenting a sheet or a custom overlay, instead of just toggling a boolean that blurs the same view.
Here are two excellent ways to fix this.
Solution 1: Using a ZStack for a Custom Overlay
This approach keeps your desired effect (a blurred background with a view on top) but structures it correctly to prevent the layout shift.
Changes:
Wrap everything in a ZStack.
The NavigationView will be the bottom layer of the ZStack. The .blur is applied here.
Add an if isShowingDetail block inside the ZStack to show your detail view on top.
You'll need a @State variable to hold the selected notification.
swift
struct ContentView: View {
@StateObject private var viewModel = NotificationViewModel()
// State to control the detail view's visibility
@State private var isShowingDetail = false
// State to hold the notification that was tapped
@State private var selectedNotification: Notification?
var body: some View {
ZStack {
// --- LAYER 1: The Main List (The Background) ---
NavigationView {
// Make sure your Notification model is Identifiable
List(viewModel.notifications) { notification in
NotificationListCell(notification: notification)
.listRowSeparator(.hidden)
.contentShape(Rectangle()) // Makes the whole cell area tappable
.onTapGesture {
self.selectedNotification = notification // Set the selected item
withAnimation {
self.isShowingDetail = true // Show the detail view
}
}
}
.navigationTitle("🔔 Notifications")
.listStyle(.plain)
}
.onAppear {
viewModel.fetchNotifications()
}
// Apply the blur to the NavigationView layer ONLY
.blur(radius: isShowingDetail ? 20 : 0) // Increased blur for better effect
// --- LAYER 2: The Detail View (The Foreground) ---
if isShowingDetail {
// This is your new detail view that appears on top
// It's a separate view, so its appearance doesn't cause the list to re-layout
NotificationDetailView(
notification: selectedNotification, // Pass the data
isShowing: $isShowingDetail // Pass a binding to dismiss it
)
.transition(.move(edge: .bottom).combined(with: .opacity))
}
}
}
}
// Example Detail View to make the code complete
struct NotificationDetailView: View {
let notification: Notification?
@Binding var isShowing: Bool
var body: some View {
VStack(spacing: 20) {
Text(notification?.title ?? "Detail View")
.font(.largeTitle)
.fontWeight(.bold)
Text(notification?.message ?? "This is the detailed message for the notification.")
.multilineTextAlignment(.center)
Button("Close") {
withAnimation {
isShowing = false
}
}
.padding()
.background(Color.blue)
.foregroundColor(.white)
.cornerRadius(10)
}
.padding(40)
.background(.thinMaterial) // A nice semi-transparent background
.cornerRadius(20)
.shadow(radius: 10)
.padding(20) // Keep it from touching the screen edges
}
}
Solution 2: Using the .sheet Modifier (More Idiomatic SwiftUI)
For a standard master-detail view, the most common and recommended SwiftUI pattern is to use a sheet. It handles the presentation and dismissal for you, and it's what users expect.
Changes:
Make your Notification model conform to Identifiable. This is best practice and required for the sheet(item: ...) modifier.
Use a single @State variable to hold the optional selectedNotification. The sheet will appear whenever this is not nil.
Attach the .sheet(item: ...) modifier to your List or NavigationView.
swift
// First, ensure your model is Identifiable
struct Notification: Identifiable {
let id = UUID() // Must have an 'id' property
let title: String
let message: String
}
struct ContentView: View {
@StateObject private var viewModel = NotificationViewModel()
// ONE state variable to control the sheet.
// If it's nil, the sheet is hidden. If it has a value, the sheet is shown.
@State private var selectedNotification: Notification?
var body: some View {
NavigationView {
List(viewModel.notifications) { notification in
NotificationListCell(notification: notification)
.listRowSeparator(.hidden)
.contentShape(Rectangle())
.onTapGesture {
// Just set the item to be presented. The sheet modifier does the rest.
selectedNotification = notification
}
}
.navigationTitle("🔔 Notifications")
.listStyle(.plain)
.onAppear {
viewModel.fetchNotifications()
}
// The sheet modifier handles showing the view
// It automatically binds to `selectedNotification`.
.sheet(item: $selectedNotification) { notification in
// This is the content of the sheet that will appear.
// You can create a dedicated view for this.
VStack {
Text(notification.title).font(.largeTitle)
Text(notification.message).padding()
// The sheet has a built-in dismiss gesture (swipe down)
}
}
}
}
}
Summary & Recommendation
Feature
ZStack Overlay (Solution 1)
.sheet Modifier (Solution 2)
Why it works
Separates background and foreground into different layout layers. Blurring one doesn't shift the other.
Presents a new view in a separate modal context. The original view's layout is unaffected.
Customization
High. You have full control over the detail view's appearance, position, and animation.
Medium. You can customize the content, but the presentation style (a card sliding up) is standard.
Complexity
More complex state management (isShowingDetail + selectedNotification).
Simpler state management (just one selectedNotification? variable).
Most use cases. This is the standard, idiomatic way to present modal information in SwiftUI.
For your situation, Solution 2 (.sheet) is generally the recommended approach unless you specifically need the custom transition and appearance from the ZStack method. It's cleaner, requires less code, and behaves in a way that iOS users are familiar with.