GrainsGames
GrainsGames

Reputation: 89

"Extra argument in call" Spacer()

I am very new to swiftui, and am making a simple game for learning reasons. I have no idea why this error would show because I did basically the same thing a couple of lines up. If you want to check the code yourself just use some random images. I cant post this post because it has to much code. it is telling me that I have to add more text so that is what I am doing right now, you do not have to read this.

import SwiftUI
struct ContentView: View {
    var body: some View {
        ZStack{
            Image("background")
                .ignoresSafeArea()         
            VStack{
                Spacer()
                Image("logo")
                Spacer()
                HStack {
                    Spacer()
                    Image("card3")
                    Spacer()
                    Image("card4")
                    Spacer()
                        }
                Spacer()
                Image("dealbutton")
                Spacer()
                HStack{
                    Spacer()
                    Text("Player")
                    Spacer()
                    Text("CPU")
                    Spacer()
                }
                .font(/*@START_MENU_TOKEN@*/.title/*@END_MENU_TOKEN@*/)
                .foregroundColor(.white)
                Spacer()
                HStack{
                    Spacer()
                    Text("0")
                    Spacer()
                    Text("0")
                    Spacer()
                }
                Spacer() //here is the error             
            }
    }
    }
    }
struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

Upvotes: 4

Views: 2766

Answers (1)

Note Spacer() is a view, and so you are exceeding the number of views (10) allowed inside another view. Try some variations of this, or use Group {...}:

struct ContentView: View {
    
    var cardView: some View {
        Group {
            Spacer()
            HStack {
                Spacer()
                Image("card3")
                Spacer()
                Image("card4")
                Spacer()
            }
            Spacer()
        }
    }
    
    var textview: some View {
        Group {
            Spacer()
            HStack{
                Spacer()
                Text("0")
                Spacer()
                Text("0")
                Spacer()
            }
            Spacer()
        }
    }
    
    var devView: some View {
        Group {
            Spacer()
            HStack{
                Spacer()
                Text("Player")
                Spacer()
                Text("CPU")
                Spacer()
            }
            Spacer()
        }
    }
    
    var body: some View {
        ZStack{
            Image("background")
                .ignoresSafeArea()
            VStack{
                Spacer()
                Image("logo")
                Spacer()
                cardView
                Image("dealbutton")
                devView
                    .font(.title)
                    .foregroundColor(.white)
                textview
                Spacer()
            }
        }
    }
}

Upvotes: 13

Related Questions