You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Xcode12(Swift5.3)嵌套NavigationView展示CoreData列表问题

Solutions to Your SwiftUI & CoreData Navigation Issues

Let's break down each problem you're facing and fix them step by step:


1. NavigationTitle Offset in AnotherView

Root Cause:

You’ve got nested NavigationView instances — StartView already wraps its content in a NavigationView, and AnotherView adds another one. This creates a double navigation bar stack, which pushes the title up incorrectly.

Fix:

Remove the extra NavigationView from AnotherView. Your list will automatically inherit the navigation context from StartView:

struct AnotherView: View {
    @Environment(\.managedObjectContext) var moc
    @FetchRequest(entity: Something.entity(), sortDescriptors: []) 
    var somethings: FetchedResults<Something>

    var body: some View {
        // Remove the outer NavigationView here
        List {
            ForEach(self.somethings, id: \.id) { something in
                NavigationLink(destination: SomethingView(somethingID: something.id)) {
                    Text(something.name ?? "unknown name")
                }
            }
        }
        .navigationBarTitle("SomethingList") // Now attaches to the parent NavigationView
    }
}

2. Unwanted White Background Around List

Root Cause:

In Xcode 12 (Swift 5.3), the default List uses a system background that might not match your parent view’s styling. The underlying UITableView also has its own background that shows through if not adjusted.

Fix:

Add these tweaks to make the List background match your desired style:

struct AnotherView: View {
    @Environment(\.managedObjectContext) var moc
    @FetchRequest(entity: Something.entity(), sortDescriptors: []) 
    var somethings: FetchedResults<Something>

    var body: some View {
        List {
            // ... your existing ForEach code ...
        }
        .navigationBarTitle("SomethingList")
        .onAppear {
            // Disable default UITableView background
            UITableView.appearance().backgroundColor = .clear
            UITableViewCell.appearance().backgroundColor = .clear
        }
        .background(Color.yourDesiredColor) // Replace with your actual background, e.g., .gray
        .listStyle(PlainListStyle()) // Optional: Use for a flat, non-grouped list look
    }
}

3. "Argument passed to call that takes no arguments" When Passing something.id

Root Cause:

Your SomethingView struct doesn’t have an initializer that accepts a somethingID parameter (or the parameter type doesn’t match what you’re passing). Even though you can access something.name (a property of your CoreData Something entity), the compiler doesn’t know how to initialize SomethingView with an ID unless you define that initializer.

Fix:

First, update SomethingView to accept the somethingID (match the type of your CoreData id property — likely UUID or String):

// Example definition for SomethingView
struct SomethingView: View {
    let somethingID: UUID // Use String if your CoreData id is a String
    
    var body: some View {
        // Your view content here
        Text("Details for ID: \(somethingID.uuidString)")
    }

    // Explicit initializer (optional but improves clarity)
    init(somethingID: UUID) {
        self.somethingID = somethingID
    }
}

If your CoreData id is optional, unwrap it safely before passing to avoid crashes:

// Hide the link if id is nil (safer than force-unwrapping)
if let id = something.id {
    NavigationLink(destination: SomethingView(somethingID: id)) {
        Text(something.name ?? "unknown name")
    }
}

All these changes should resolve the issues you’re seeing in Xcode 12 with Swift 5.3. Feel free to tweak the background color or list style to match your app’s design!

内容的提问来源于stack exchange,提问作者ccd

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.08 22:12:52