0

I want to set shadow to parent view. But I want child views to remain the same.

What I do now is

parentView.layer.shadowRadius = 0.8;
parentView.layer.shadowOpacity = 0.3;
parentView.layer.shadowOffset = CGSizeMake(1.0, 1.4);

However, if I do this, the child views are also changed. Is there a way to set the shadow but keep the child views the same.

Thank you

user3786609
  • 211
  • 2
  • 11

1 Answers1

14

Is your parentView by any chance transparent? I've set up a simple project and used your code. I've changed some values to see a shadow a little better. This is how it looks:

enter image description here

As you can see - no shadow on the subview. parentView's background color is set to white. When I set it to clear color this is what happens. I added the border to prove that parentView is still there:

enter image description here

Apple Docs prove this:

Figure A-7 shows several different versions of the same sample layer with a red shadow applied. The left and middle versions include a background color so the shadow appears only around the border of the layer. However, the version on the right does not include a background color. In this case, the shadow is applied to the layer’s content, border, and sublayers.

enter image description here

NKorotkov
  • 3,591
  • 1
  • 24
  • 38