Learn Courses My Dashboard

Why doesn't Chris' code work for me?

I am using Xcode 12.5.

Reference: I am trying to do the IOS Foundations Swift UI: Mod 4: Lesson 8 challenge. The challenge is to create scrollable view of identical rectangles with rounded corners. The rectangles must have a gradient fill.

Problem: When I recreate the code Chris has in the solution, Xcode does not accept the .fill modifier. I was able to get a gradient background using .background, but Xcode would not accept .foreground.

Question 1: Why does Xcode accept .fill for Chris and not for me when we have exactly the same code?

Question 2: How can I get the gradient effect to fill the rectangle if I can’t use .fill? is there another modifier that might work?

The solution supplied by Chris Ching should work. I tested it on my Mac and It works for me.

Copy and paste the code below into a new project and see if that works.

struct ContentView: View {
    var body: some View {
        ScrollView {
            LazyVStack(alignment: .leading) {
                ForEach(0..<60) { index in
                                gradient: Gradient(colors: [.orange, .red]),
                                center: UnitPoint(x: 0.5, y: 0.5),
                                startRadius: 0,
                                endRadius: 200))
                        .frame(height: pow(1.1, CGFloat(index)))

Thank you, Chris. Your solution mostly worked. It produced a list off rectangles all of the same width, but of increasing heights. I was able to fix that by changing the frame to a fixed height and width.

The challenge is to have gradually increasing heights based on the index value so the code I supplied is what you need to have. Of course you can customise it anyway you prefer.

Interesting because in the pic shown in the challenge the rectangles are all the same size. I did have fun playing with the gradient though. I created a whole bunch of colors, and then after I thought about it, I decided that I would hate to look at an app with such a crazy background! :grin: But it’s good to know. Thanks again! You’re always so helpful and do a great job explaining things. I always understand your responses the best.

1 Like