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

时间:2019-09-03 12:48:33

标签: ios swift swiftui

给出以下示例代码:

@Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        MediaPlayer mediaSong = MediaPlayer.create(this, R.raw.music_box_ringtone);
        mediaSong.setLooping(true);
        mediaSong.start();
        return START_STICKY;
    }

如何找出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? } } 的当前值以便正确呈现UIView?

我已经读过this question,但这是从外部而不是包装的View检查外部的ModifiedContent的角度来看的。

1 个答案:

答案 0 :(得分:0)

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

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
        }

    }
}