繁体   English   中英

在 SwiftUI 中,如何访问 UIViewRepresentable 中 .foregroundColor 和其他修饰符的当前值?

[英]In SwiftUI, how can I access the current value of .foregroundColor and other modifiers inside my UIViewRepresentable?

给出以下示例代码:

struct ActivityIndicatorView : UIViewRepresentable {
    var style: UIActivityIndicatorView.Style = .medium

    func makeUIView(context: UIViewRepresentableContext<ActivityIndicatorView>) -> UIActivityIndicatorView {
        return UIActivityIndicatorView(style: style)
    }

    func updateUIView(_ uiView: UIActivityIndicatorView, context: UIViewRepresentableContext<ActivityIndicatorView>) {
        uiView.color = UIColor.white // how would I set this to the current .foregroundColor value?
    }
}

如何找出.foregroundColor(…)的当前值以正确呈现我的 UIView?

我已经阅读了这个问题,但这是从外部检查 ModifiedContent 的角度来看的,而不是包装的 View。

无法访问前景色,但是您可以访问配色方案并基于此来确定活动指示器的颜色:

struct ActivityIndicatorView : UIViewRepresentable {

   @Environment(\.colorScheme) var colorScheme: ColorScheme

    //...

    func updateUIView(_ uiView: UIActivityIndicatorView, context: UIViewRepresentableContext<ActivityIndicatorView>) {

        switch colorScheme {
        case .dark:
            uiView.color = .white
        case .light:
            uiView.color = .black
        }

    }
}

同意,应该解决这个问题。 我尝试将这个值从环境中提取出来,这就是我得到的。 看起来 EnvironmentKey 存在,但是是internal :( 在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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