1

I have a UIView which I want to have a shadow and round corners. The problem is - a shadow requires masksToBounds = NO while round corners requires masksToBounds = YES.

The solution I found is to have a container which will have shadow and add my UIView as a subview of the container - and give it rounded corners.

This works. I have both shadow AND round corners - but it's no good. The shadow is of a rectangle view and my image has rounded corners.

Shadow and rounded corners

How can I implement a shadow for the rounded corners?

YogevSitton
  • 10,068
  • 11
  • 62
  • 95
  • See this link http://stackoverflow.com/questions/1509547/uiview-with-rounded-corners – Yogendra Jul 24 '14 at 08:07
  • I don't have a problem applying rounded corners. The problem is that rounded corners along with shadow doesn't look good... – YogevSitton Jul 24 '14 at 08:32
  • Use images masking the corners. It's far simpler, faster to code and usually better looking than any programming solution. – Sulthan Jul 24 '14 at 08:35
  • My problem is that the cell can change it's size from time to time using animation. Using images will make my life even harder. – YogevSitton Jul 24 '14 at 10:42

3 Answers3

1
import UIKit

@IBDesignable
class customButton: UIView {

    @IBInspectable var cornerRadius:CGFloat = 0{
        didSet{
            self.layer.cornerRadius = cornerRadius
        }
    }

    @IBInspectable var borderWidth:CGFloat = 0{
        didSet{
            self.layer.borderWidth = borderWidth
        }
    }

    @IBInspectable var borderColor:UIColor = UIColor.white{
        didSet{
            self.layer.borderColor = borderColor.cgColor
        }
    }


}

Set this as class for the UIView and put maskToBound = true. Then in the inspector set the value accordingly to get a rounded shadow.

Ayush Verma
  • 169
  • 1
  • 7
0

Remove the container, its clipping your view shadow. Import QuartzCore/QuartzCore.h . And try this code to your view

#import < QuartzCore/QuartzCore.h>

...

view.layer.cornerRadius = 5.0f;
[view.layer setShadowColor:[UIColor redColor].CGColor];
[view.layer setShadowOpacity:0.7];
[view.layer setShadowRadius:5.0];
[view.layer setShadowOffset:CGSizeMake(2.0, 2.0)];

to know more about maskToBound see this link

What UIView layer.masksToBounds is doing if set to YES?

Community
  • 1
  • 1
Yogendra
  • 1,728
  • 16
  • 28
-2
// border radius
[yourView.layer setCornerRadius:30.0f];

// border
[yourView.layer setBorderColor:[UIColor blackColor].CGColor];
[yourView.layer setBorderWidth:1.5f];
 yourView.layer.masksToBounds=YES;

// drop shadow
[yourView.layer setShadowColor:[UIColor lightGrayColor].CGColor];
[yourView.layer setShadowOpacity:0.8];
[yourView.layer setShadowRadius:3.0];
[yourView.layer setShadowOffset:CGSizeMake(2.0, 2.0)];

This may help you.

Iphonenew
  • 299
  • 2
  • 11