如何在 SwiftUI 中访问视图的修饰符? [英] How can I access modifiers of a view in SwiftUI?

查看:30
本文介绍了如何在 SwiftUI 中访问视图的修饰符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个带有 ImageView,它有一个 shadow 属性:

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:

'(Color, Length, Length, Length) -> _ModifiedContent<_ModifiedContent, _ShadowEffect>' (aka '(Color, CGFloat, CGFloat, CGFloat) -> _ModifiedContent<_ModifiedContent, _ShadowEffect>') 类型的值没有成员'半径'

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?

推荐答案

myImage的返回类型为:

_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)

这篇关于如何在 SwiftUI 中访问视图的修饰符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆