1

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?

Maharajan S
  • 93
  • 10

1 Answers1

4

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

ScottM
  • 7,108
  • 1
  • 25
  • 42
  • yup Thanks, using this method I got the result what i want, but i need to check for each subviews background Color and we need to declare environment in each struct view. is there a way to be written Environment in common class. – Maharajan S Nov 25 '21 at 09:57
  • I don't think there is, really. When you're building a custom view that needs to work with redaction, you can leave it to your subviews – texts, images, etc. – to implement their default behaviour or. But when that doesn't work , and the default redaction leaves in elements which you don't want, like background colours, you have to step in and tell your own views how to redact themselves. – ScottM Nov 25 '21 at 11:25
  • If you find yourself writing the same custom redaction code over and over, you could extract that common code – say, create your own `redactableBackground` modifier and use that instead of `background`. That would cut down on the amount of custom redaction code scattered around the place. – ScottM Nov 25 '21 at 11:26
  • 'redactableBackground' will be good idea. I will try that – Maharajan S Nov 25 '21 at 11:59