On line 13 I keep getting the error “Cannot find ‘BackgroundView’ in scope. I tried searching if it had something to do with the folders, and I tried making a new project where I simplified everything and got rid of the UI folder and it worked but I wont to keep my things organized.
File structure:
Once More
├── Once_MoreApp.swift
├── ContentView.swift
├── UI
│ ├── BackgroundView.swift
│ ├── OtherViews.swift
…
// ContentView.swift
import SwiftUI
struct ContentView: View {
var body: some View {
NavigationView {
VStack {
NavigationLink(destination: BackgroundView()) {
Text("Go to BackgroundView")
}
}
.navigationBarTitle("Main View")
}
.padding()
}
}
#Preview {
ContentView()
}
//Background view
import SwiftUI
struct BackgroundView<Content: View>: View {
let content: Content
init(@ViewBuilder content: () -> Content) {
self.content = content()
}
var body: some View {
ZStack {
Image("background")
.resizable()
.edgesIgnoringSafeArea(.all)
content
}
}
}
I simplified the file structure and got rid of the UI folder, and this worked. BackgroundView was able to be called from contentView
Michael is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.