Shadow left and right of UIView

22,651

Solution 1

I think 10 is a pretty big shadow radius, try 3 or 4 instead, also opacity I usually use 0.7:

someView.layer.shadowColor = [[UIColor blackColor] CGColor];
someView.layer.shadowOffset = CGSizeMake(0.0f,0.0f);
someView.layer.shadowOpacity = 0.7f;
someView.layer.shadowRadius = 4.0f;

If you want the shadow only on left and right, then inset the rectangle on the top and bottom so the top and bottom shadow are hidden behind your view:

CGRect shadowRect = CGRectInset(someView.bounds, 0, 4);  // inset top/bottom
someView.layer.shadowPath = [[UIBezierPath bezierPathWithRect:shadowRect] CGPath];

I'm not really sure if that's what you wanted.

Solution 2

swift 3.0 version

imageView.layer.shadowOpacity = 0.8
imageView.layer.shadowOffset = CGSize(width: 0, height: 3)
imageView.layer.shadowRadius = 4.0

let shadowRect: CGRect = imageView.bounds.insetBy(dx: 0, dy: 4)
imageView.layer.shadowPath = UIBezierPath(rect: shadowRect).cgPath
Share:
22,651
Zeropointer
Author by

Zeropointer

Updated on May 06, 2021

Comments

  • Zeropointer
    Zeropointer almost 3 years

    Hi i like to add an CALayer Shadow to an View where the shadow was left and right of the view the simplest way was:

    someView.layer.shadowColor = [[UIColor blackColor] CGColor];
    someView.layer.shadowOffset = CGSizeMake(0.0f,0.0f);
    someView.layer.shadowOpacity = 1.0f;
    someView.layer.shadowRadius = 10.0f;
    someView.layer.shadowPath = [[UIBezierPath bezierPathWithRect:someView.bounds] CGPath];
    

    but i will add a bigger shadow as like a shadow when i increase the shadowRadius it do not look good. How i can make a shadow that looks good at left and right.

  • Zeropointer
    Zeropointer over 11 years
    I have talked with my designer this shadow is to small. I need different x offsets on left and right.
  • l.vasilev
    l.vasilev almost 8 years
    Amazing answer, But guys have somebody implemented this with rounded corners(more smooth), since this rect-like shadow looks bad. Any insights ? Thanks in advance ! :)
  • progrmr
    progrmr almost 8 years
    Use bezierPathWithRoundedRect instead
  • mattsson
    mattsson about 7 years
    I'm not sure why this has so many votes. In my implementation, as expected, it prevents the side shadows from going all the way to the top and bottom of the sides.
  • Mitesh Dobareeya
    Mitesh Dobareeya about 6 years
    @progrmr I want inner shadow with specific side, like only top or top/right. Any suggestions ?