3
votes

I have 2 swift files:

ContentView.swift

struct ContentView: View {
    var body: some View {
        NavigationView {
            ZStack(alignment: .leading) {

                Color.white.edgesIgnoringSafeArea(.all)

                SetBackground
            }
        }
    }
}

and the swiftui view file I created called:

Background.swift

struct SetBackground: View {
    var body: some View {
        GeometryReader { geometry in
            Capsule()
                .foregroundColor(.yellow)
                .frame(width: geometry.size.width * 1.7)
                .offset(x: geometry.size.width * -0.1 , y: geometry.size.height * -0.9)
        }
    }
}

When I try to call the file Background in the 1st code, I get the error: Type 'SetBackground.Type' cannot conform to 'View'; only struct/enum/class types can conform to protocols

Why is this and how could I fix it?

Thanks

1

1 Answers

2
votes

You have to use the View by initializing the object rather than using the SetBackground type.

struct ContentView: View {
    @State var dataStore = [0, 1, 2]
    @State var a = ""

    var body: some View {
        NavigationView {
            ZStack(alignment: .leading) {

                Color.white.edgesIgnoringSafeArea(.all)

                SetBackground() <---- add "()"
            }
        }
    }
}