SwiftUI - Value of type 'some View' has no member 'resizable'
Asked Answered
F

3

6

I'm new to SwiftUI coding and trying to learn step by step. Sometimes I don't get what XCode is trying to tell me with its errors. Following a super simple tutorial on how to resize an image. Did everything ok until this error came up:

Value of type 'some View' has no member 'resizable'

I'm trying to resize an image in my assets folder. This is my code:

import SwiftUI

struct ContentView: View {

...
...

 var body: some View {

...

 Image("home_illustration")
                        .padding(.bottom,60)
                        .resizable()
...

 }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

Frenetic answered 9/12, 2022 at 9:46 Comment(0)
P
4

Each SwiftUI modifier returns a type. The final type of a View's body can be extremely complex, hence the existence of some View - that basically tells the compiler that you're returning something that conforms to the View protocol, but that you don't have the time or the energy to spell out exactly what it is, and anyway that's your job, you're a compiler, just figure it out.

Some modifiers only make sense when applied to certain types of view. In your case, .padding can be added to anything, so it's a method on the View protocol. It returns a type of ModifiedContent<SwiftUI.Image, SwiftUI._PaddingLayout>. These types build up the more modifiers you apply.

.resizable() is a method on Image, which returns an Image.

So if you resize, then pad the image, you're OK. If you apply padding, you aren't dealing with an image any more, so the compiler can't figure out what is happening.

Pinhead answered 9/12, 2022 at 10:27 Comment(1)
Thank you for the explanation! It's really important to find out more about how things work!Frenetic
C
5

you have to use .resizable() above the .padding and below the Image. The reason is Image with padding is create a new child view so we can't apply .resizable() to view. It is for Image only. Let me know if you are not clear with my answer.

Caird answered 9/12, 2022 at 10:25 Comment(1)
Thank you for the answer! As wrote in my self-post, I've found out about playing randomly with the modifiers, but your answer is definitely teaching me something new!Frenetic
P
4

Each SwiftUI modifier returns a type. The final type of a View's body can be extremely complex, hence the existence of some View - that basically tells the compiler that you're returning something that conforms to the View protocol, but that you don't have the time or the energy to spell out exactly what it is, and anyway that's your job, you're a compiler, just figure it out.

Some modifiers only make sense when applied to certain types of view. In your case, .padding can be added to anything, so it's a method on the View protocol. It returns a type of ModifiedContent<SwiftUI.Image, SwiftUI._PaddingLayout>. These types build up the more modifiers you apply.

.resizable() is a method on Image, which returns an Image.

So if you resize, then pad the image, you're OK. If you apply padding, you aren't dealing with an image any more, so the compiler can't figure out what is happening.

Pinhead answered 9/12, 2022 at 10:27 Comment(1)
Thank you for the explanation! It's really important to find out more about how things work!Frenetic
F
0

Found the solution randomly by playing with my code. The problem was the order of the modifiers.

"resizable()" should be always first.

Don't ask me why but...it just works. Hopefully, this will help others.

Frenetic answered 9/12, 2022 at 10:12 Comment(1)
Resizable only works on an image, once padding is applied it is “some View” not an imageTaradiddle

© 2022 - 2024 — McMap. All rights reserved.