Challenge: Views & Modifiers | raywenderlich.com


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/18272812-swiftui-fundamentals/lessons/4

Hi @catie,
None of the project files show the automatic preview for some reason. The project builds fine and runs properly in the Xcode simulator, but it’s the automatic preview that is not working. I’m using the latest Xcode Version 12.3 (12C33). I’ve tried cleaning the project, but that didn’t help. The automatic preview complains of compilation failures in the preview pane. The error message doesn’t make sense because it is complaining about unterminated string literals in a cryptic way. There aren’t any errors because it builds and runs fine on the simulator. The automatic preview does work if I start a project from scratch.

Any idea what may be wrong?

  1. in challange 2 you use:

     .resizable()
     .frame(width: 50, height: 50)
    

but I got similar effect using just:

        .scaleEffect(3.0)

is there any reason not to use scaleEffect? I mean is your method more efficient?

  1. then you use twice:

     .foregroundColor(.white)
    

on each text. I’ve stacked both text in VStack and use foregroundColor once.

is there any reason not to use more VStacks?

Hiya. I haven’t been able to reproduce this with the provided projects, but if it’s only failing in the preview, I’d say double check the previews code in that challenge. Also a general SwiftUI preview “magic” fix: try setting a different simulator, then reloading the preview.

Hi! Good questions!

  1. I wouldn’t say either is more efficient, but they are doing different things. .scaleEffect only changes the rendered size of the image, not the frame. So changing the scale shouldn’t affect the surrounding views in the same way changing the frame will. You can see this in action by highlighting the image with each modifier on, and the preview will show you the frame’s current bounding box. When you use .scaleEffect, the image is extending outside of its frame:

scaleEffect
frame

  1. No reason not to use another VStack! That’s a fine approach :]