简体   繁体   中英

How can I access modifiers of a view in SwiftUI?

Assume I have a View with an Image that has a shadow property:

struct ContentView: View {
    var body: some View {
        let myImage = Image("turtlerock").shadow(radius: 10)

        return myImage
    }
}

Now imagine I want to access the value of the shadow radius. I assumed I could do this:

print(myImage.shadow.radius)

However, this returns an error:

Value of type '(Color, Length, Length, Length) -> _ModifiedContent<_ModifiedContent, _ShadowEffect>' (aka '(Color, CGFloat, CGFloat, CGFloat) -> _ModifiedContent<_ModifiedContent, _ShadowEffect>') has no member 'radius'

Is there a way to access the modifier?

The return type of myImage is:

_ModifiedContent<Image, _ShadowEffect>

We can access the original image by doing:

myImage.content

We can access the shadow effect modifier by typing:

myImage.modifier

So to do what you want, you have to type:

print(myImage.modifier.radius)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM