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

3.3k Views Asked by At

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()
    }
}

3

There are 3 best solutions below

1
jrturton On BEST ANSWER

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.

1
helloimbrando On

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.

1
darshilsakhiya On

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.