In this article we will learn how to add drop shadow to an Image view.
Step 1:
Add the following UIImageView extension:extension UIImageView {
func dropShadow() {
self.layer.masksToBounds = false
self.layer.shadowColor = UIColor.black.cgColor
self.layer.shadowOpacity = 0.5
self.layer.shadowOffset = CGSize(width: -1, height: 1)
self.layer.shadowRadius = 1
self.layer.shadowPath = UIBezierPath(rect: self.bounds).cgPath
self.layer.shouldRasterize = true
self.layer.rasterizationScale = UIScreen.main.scale
}
}
Step 2:
We can use as follow:let imageView = UIImageView() imageView.frame = CGRect(x: view.frame.width/2 - 128, y: view.frame.height/2 - 80, width: 256, height: 160) imageView.dropShadow() imageView.image = UIImage(named: "Tiger.png") imageView.backgroundColor = UIColor.white view.addSubview(imageView)

No comments:
Post a Comment