UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: What GeometryProxy Parameter Do I Pass Into My Preview So My Code Will Build?

Forums > SwiftUI

I have a ContentView that has the following code:

struct ContentView: View {  
    var body: some View {
        GeometryReader { geometry in
            VStack {
                ForEach(0..<9) { num in
                    GameTiles(proxy: geometry)
                }
            }
        }
    }
}

I have second view (separate file) that I refactored out of my content view. The code is:

    struct GameTiles: View {
        var proxy: GeometryProxy
        var body: some View {
            RoundedRectangle(cornerRadius: 10)
                .foregroundColor(.blue)
                .frame(width: proxy.size.width/3.6, height: proxy.size.width/3.6)
        }
    }

struct GameTiles_Previews: PreviewProvider {
    static var previews: some View {
        GameTiles(proxy: //ISSUE: What parameter can I pass into proxy? )
    }
}

I have tried a couple of arguments in my preview to build my code, but none of them are of expected argument type 'GeometryProxy'

What can I use as an argument for my preview so my code builds?

2      

If all you need in GameTiles is the proxy's width, why not just pass that property as a CGFloat:

struct ContentView: View {
    var body: some View {
        GeometryReader { geometry in
            VStack {
                ForEach(0..<9) { num in
                    GameTiles(width: geometry.size.width)
                }
            }
        }
    }
}
struct GameTiles: View {
    let width: CGFloat
    var body: some View {
        GeometryReader {proxy in
            RoundedRectangle(cornerRadius: 10)
                .foregroundColor(.blue)
                .frame(width: width/3.6, height: width/3.6)
        }
    }
}

struct GameTiles_Previews: PreviewProvider {
    static var previews: some View {
        GameTiles(width: 100)
    }
}

Then you can use whatever value you want in the preview.

2      

A preview isn't the real view. Just a working representation of what this smaller piece of your solution looks like.

So in this case, maybe don't worry about "passing in" a realistic value?

Try wrapping your GameTiles(proxy:) in its own GeometryReader.

struct GameTiles_Previews: PreviewProvider {
    static var previews: some View {
        GeometryReader { previewProxy in
        // Normally passed in by the parent view. 
        // No parent view in previews! So fake it.
            GameTiles(proxy:  previewProxy)
        }
    }
}

GameTiles(proxy:) is the only link to your real application. Everything else in a preview is scaffolding.

2      

@Roosterboy - Appreciate you chiming in! The example I posted was a very simplified version of my actual project - I'm pulling multiple views out of a GeometryReader. I should have clarified this in my original post.

@Obelix - This worked great in my project! Thank you for helping me understand the preview better :)

3      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.