Search code examples
swiftuiplaceholdershimmer

How to change the Color of SwiftUI redacted shimmer effect


I added a shimmer effect for my home screen like the code below.

VStack{
  TopView()
  HomeView()
}
.redacted(.placeholder)

I got the output like the below image.

https://ibb.co/xFXQ2sk

the shimmer view showing color for the colored view but the normal view shows gray colored.

can i change the whole shimmer view into gray color without showing the colored view in shimmer?


Solution

  • One approach might be to adjust your custom subviews to respond to whether they're being asked to display as a redaction.

    For example, if you have a view that adds a piece of text within a red background, e.g.:

    struct LabelView: View {
      var text: String
    
      var body: some View {
        Text(text)
           .padding()
           .background(Color.red)
      }
    }
    

    you could then adapt it to use grey when being redacted:

    struct LabelView: View {
      @Environment(\.redactionReasons) var redactionReasons
      var text: String
    
      var body: some View {
        Text(text)
           .padding()
           .background(redactionReasons.contains(.placeholder) ? Color.gray : Color.red)
      }
    }