Given the following example code:
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?
}
}
How do I find out the current value of .foregroundColor(…)
in order to render my UIView correctly?
I have read this question but that is from the perspective of inspecting the ModifiedContent from the outside, not the wrapped View.
A wrapper for a UIKit view that you use to integrate that view into your SwiftUI view hierarchy.
Adding custom colors is actually simple, you add the color to the assets folder and then you write a few lines of code to use it anywhere you want in your project. Search for the assets folder in your project, once inside you'll see everything that's added to the folder such as images, your app icon, etc.
There is no way to access the foreground colour but you can access the colour scheme and determine the colour of your activity indicator based on that:
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
}
}
}
Agreed, this should be addressed. I took a stab at trying to pull this value out of the Environment and this is what I got. Looks like the EnvironmentKey exists, but is internal
:(
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With