iosswiftuiresizablexcode14

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


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


Solution

  • 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.