Xcode12(Swift5.3)嵌套NavigationView展示CoreData列表问题
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

