UIView with rounded corners and drop shadow?
The following code snippet adds a border, border radius, and drop shadow to v
, a UIView
:
// border radius
[v.layer setCornerRadius:30.0f];
// border
[v.layer setBorderColor:[UIColor lightGrayColor].CGColor];
[v.layer setBorderWidth:1.5f];
// drop shadow
[v.layer setShadowColor:[UIColor blackColor].CGColor];
[v.layer setShadowOpacity:0.8];
[v.layer setShadowRadius:3.0];
[v.layer setShadowOffset:CGSizeMake(2.0, 2.0)];
Swift 5 Version :
// border radius
v.layer.cornerRadius = 30.0
// border
v.layer.borderColor = UIColor.lightGray.cgColor
v.layer.borderWidth = 1.5
// drop shadow
v.layer.shadowColor = UIColor.black.cgColor
v.layer.shadowOpacity = 0.8
v.layer.shadowRadius = 3.0
v.layer.shadowOffset = CGSize(width: 2.0, height: 2.0)
You can adjust the settings to suit your needs.
Also, add the QuartzCore framework to your project and:
#import <QuartzCore/QuartzCore.h>
See my other answer regarding masksToBounds
.
Note
This may not work in all cases. If you find that this method interferes with other drawing operations that you are performing, please see this answer.
How to round top two corners of UIView and add shadow above?
SWIFT 5: iOS 11 introduced maskedCorners
which results in smoother and better quality results. You can still use the UIRectCorner
in the function call and have it translated to CACornerMask
:
extension UIView {
func roundCorners(_ corners: UIRectCorner, radius: CGFloat) {
if #available(iOS 11.0, *) {
clipsToBounds = true
layer.cornerRadius = radius
layer.maskedCorners = CACornerMask(rawValue: corners.rawValue)
} else {
let path = UIBezierPath(
roundedRect: bounds,
byRoundingCorners: corners,
cornerRadii: CGSize(width: radius, height: radius)
)
let mask = CAShapeLayer()
mask.path = path.cgPath
layer.mask = mask
}
}
func addShadow(shadowColor: CGColor = UIColor.label.cgColor,
shadowOffset: CGSize = CGSize(width: 1.0, height: 2.0),
shadowOpacity: Float = 0.4,
shadowRadius: CGFloat = 3.0) {
self.layer.shadowColor = shadowColor
self.layer.shadowOffset = shadowOffset
self.layer.shadowOpacity = shadowOpacity
self.layer.shadowRadius = shadowRadius
self.layer.masksToBounds = false
}
}
These functions need to be applied in layoutSubviews() of your superview.
override func layoutSubviews() {
roundCorners([.topLeft, .topRight], radius: 15)
addShadow(shadowColor: UIColor.text1.cgColor, shadowOffset: CGSize(width: 0, height: -3), shadowOpacity: 0.2, shadowRadius: 5)
}
How to add inner shadow to UIView with rounded corners
It's a trick. You give shadow and border to same view, the shadow will fall inside the view. please make sure the background color of view is clear. use the below code for reference.
yourView.layer.shadowColor = UIColor.gray.cgColor
yourView.layer.shadowOpacity = 0.3
yourView.layer.shadowOffset = CGSize.zero
yourView.layer.shadowRadius = 6
yourView.layer.masksToBounds = true
yourView.layer.borderWidth = 1.5
yourView.layer.borderColor = UIColor.white.cgColor
yourView.layer.cornerRadius = imageView.bounds.width / 2
UIView with shadow, rounded corners and custom drawRect
This is a tricky one. UIView
's clipsToBounds
is necessary to get the rounded corners. But CALayer
's masksToBounds
has to be false
so the shadow is visible. Somehow, everything works if drawRect
is not overridden, but actually it shouldn't.
The solution is to create a superview to provide the shadow (in the demonstration below this is the shadowView
). You can test the following in Playground:
class MyView : UIView {
override func drawRect(rect: CGRect) {
let c = UIGraphicsGetCurrentContext()
CGContextAddRect(c, CGRectMake(10, 10, 80, 80))
CGContextSetStrokeColorWithColor(c , UIColor.redColor().CGColor)
CGContextStrokePath(c)
}
}
let superview = UIView(frame: CGRectMake(0, 0, 200, 200))
let shadowView = UIView(frame: CGRectMake(50, 50, 100, 100))
shadowView.layer.shadowColor = UIColor.blackColor().CGColor
shadowView.layer.shadowOffset = CGSizeZero
shadowView.layer.shadowOpacity = 0.5
shadowView.layer.shadowRadius = 5
let view = MyView(frame: shadowView.bounds)
view.backgroundColor = UIColor.whiteColor()
view.layer.cornerRadius = 10.0
view.layer.borderColor = UIColor.grayColor().CGColor
view.layer.borderWidth = 0.5
view.clipsToBounds = true
shadowView.addSubview(view)
superview.addSubview(shadowView)
Result:
rounded edges with a shadow
Add dummy container view and assign it the shadow, after that put inside of it the image view like this:
Declare your objects under your class controller:
let imgV = UIImageView()
let dummyView = UIView()
after that in viewdidLoad set property and constraints:
imgV.image = UIImage(named: "yourImage")
imgV.contentMode = .scaleToFill
imgV.layer.cornerRadius = 20
imgV.clipsToBounds = true
imgV.translatesAutoresizingMaskIntoConstraints = false
dummyView.center = self.view.center
dummyView.backgroundColor = UIColor.yellow
dummyView.layer.shadowColor = UIColor.gray.cgColor
dummyView.layer.shadowOpacity = 1
dummyView.layer.shadowOffset = CGSize(width: 2, height: 2)
dummyView.layer.shadowRadius = 5
dummyView.layer.cornerRadius = 20
dummyView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(dummyView)
dummyView.heightAnchor.constraint(equalToConstant: 200).isActive = true
dummyView.widthAnchor.constraint(equalToConstant: 200).isActive = true
dummyView.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
dummyView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
dummyView.addSubview(imgV)
imgV.topAnchor.constraint(equalTo: dummyView.topAnchor).isActive = true
imgV.bottomAnchor.constraint(equalTo: dummyView.bottomAnchor).isActive = true
imgV.leadingAnchor.constraint(equalTo: dummyView.leadingAnchor).isActive = true
imgV.trailingAnchor.constraint(equalTo: dummyView.trailingAnchor).isActive = true
this is the result
Swift uiview with rounded corners and drop shadow does not work
I would try putting your VC layer code in viewDidLayoutSubviews instead of viewDidLoad. Your view will be properly sized by that point.
How to add shadow to UIView that has round corners
I do believe the issue is with clipping the bounds. If you want to have the view clip the bounds, you should have two views - one for the shadow and one for the content.
Does this work for you?
// corner radius
featureOneView.layer.cornerRadius = 10
// shadow
featureOneView.layer.shadowColor = UIColor.black.cgColor
featureOneView.layer.shadowOffset = CGSize(width: 3, height: 3)
featureOneView.layer.shadowOpacity = 0.6
featureOneView.layer.shadowRadius = 3.0
A quick google search provided: https://stackoverflow.com/a/34984063/6885097
Related Topics
Swift - Json Error: the Data Couldn'T Be Read Because It Isn'T in the Correct Format
Wkwebview Origin Null Is Not Allowed by Access-Control-Allow-Origin
How to Check For an Active Internet Connection on iOS or Macos
Vertically Align Text to Top Within a Uilabel
Nsnotificationcenter Addobserver in Swift
How to Use Nsjsonserialization
Generate .Pem File Used to Set Up Apple Push Notifications
How to Add Uitableview Within a Uitableviewcell
How to Trigger a Block After a Delay, Like -Performselector:Withobject:Afterdelay:
Wkwebview Not Loading Local Files Under iOS 8
Detect Tap on a Button in Uitableviewcell for Uitableview Containing Multiple Sections
How to Sort an Nsmutablearray With Custom Objects in It
Converting Nsstring to Nsdate (And Back Again)