35

I have to drop a shadow to the right and bottom of uiview.Im doing this in interface builder.But I see the shadow dropped to top of it.Tried differnt sizes.but couldn't get it.

layer.masksToBound=No
layer.shadowOpacity=0.15
layer.shadowRadius=2
layer.shadowOffSet={10,-10}   //Values being set in Interfacebuilder.

Still this drops shadow at top.What should I do to get at bottom of view.

1
  • for runtime @walle84 is corrent answer.
    – Joker
    Apr 18, 2014 at 8:48

5 Answers 5

116

Try the following code, it might help you

    myView.layer.shadowColor = [UIColor purpleColor].CGColor;
    myView.layer.shadowOffset = CGSizeMake(5, 5);
    myView.layer.shadowOpacity = 1;
    myView.layer.shadowRadius = 1.0;
    myView.layer.maskToBounds = NO;

I tested this code and it's working and output is:

enter image description here

4
  • 3
    Can u tell me how to add shadow only in bttom of the view. Nov 29, 2014 at 5:46
  • 7
    Instead of myView.layer.shadowOffset = CGSizeMake(5, 5); do myView.layer.shadowOffset = CGSizeMake(0, 5); Feb 25, 2015 at 12:02
  • inimageview i can't add this shadow. i have coded like as you write. Jul 18, 2016 at 13:46
  • 1
    Hey, I tried setting my cgsizemake(0,n), but i keep getting a shadow on the right side of the view... any idea why that is? Aug 16, 2016 at 18:45
6

Hi I have used below code ,it will provide you with shadow you want.

 UIBezierPath *shadowPath = [UIBezierPath bezierPathWithRect:_viewShadow.bounds];
_viewShadow.layer.masksToBounds = NO;
_viewShadow.layer.shadowColor = [UIColor blackColor].CGColor;
_viewShadow.layer.shadowOffset = CGSizeMake(10.0f, 5.0f);  /*Change value of X n Y as per your need of shadow to appear to like right bottom or left bottom or so on*/
_viewShadow.layer.shadowOpacity = 0.5f;
_viewShadow.layer.shadowPath = shadowPath.CGPath;

Also masksToBounds is imp as it disables the clipping of sublayers that extend further than the view's bounds. If you put it YES then you won't see shadow as it clips sublayer where else in NO it allow to extent layer.

1
  • 1
    Thanks for your note on 'masksToBounds', I was missing this one. Apr 26, 2017 at 7:39
3

In Swift 3, CGSizeMake no longer exists. It has been changed to CGSize(width: 20, height: 10). So the shadowOffset can be set like this in Swift 3:

myView.layer.shadowOffset = CGSize(width: 20, height: 10)
1

I found out that these values give a nice result :

myView.layer.shadowColor = UIColor.black.cgColor
myView.layer.shadowOpacity = 0.25
myView.layer.shadowRadius = 3
myView.layer.shadowOffset = CGSize(width: 1, height: 1) // shadow on the bottom right

enter image description here

0

I think your shadow offset is incorrect. It should be { 10 , 10} like:

[layer setShadowOffset:CGSizeMake( 10 , 10 ) ];
2
  • Use code to create shadows. check this link Apr 18, 2014 at 7:50
  • @GauravSingh i used this to draw shadow for tableview cell but after scrolling up it goes to top. Aug 9, 2016 at 12:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.