How can I access modifiers of a view in SwiftUI?

2019-07-11 13:02发布

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?

标签: swift swiftui
1条回答
爱情/是我丢掉的垃圾
2楼-- · 2019-07-11 13:44

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)
查看更多
登录 后发表回答