Skip to content

Instantly share code, notes, and snippets.

@alexnikol
Created April 27, 2020 17:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexnikol/10541c882268bdacdf16ee9fe72205b6 to your computer and use it in GitHub Desktop.
Save alexnikol/10541c882268bdacdf16ee9fe72205b6 to your computer and use it in GitHub Desktop.
How to Create UIView with shadow, image, border and cornerRadius
let rect = CGRect(x: 40, y: 40, width: 300, height: 250)
let view = UIView(frame: rect)
view.backgroundColor = UIColor.systemPink
//Add Shadow
view.layer.shadowColor = UIColor.red.cgColor
view.layer.shadowOffset = CGSize(width: 20, height: 20)
view.layer.shadowRadius = 10.0
view.layer.shadowOpacity = 0.7
//Add cornerRadius
view.layer.cornerRadius = 20.0
view.layer.borderColor = UIColor.gray.cgColor
view.layer.borderWidth = 4.0
//Add image as CALayer
let image2 = UIImage(named: "evening.jpeg")
let imageLayer = CALayer()
imageLayer.contents = image2?.cgImage
imageLayer.frame = view.bounds
imageLayer.masksToBounds = true //Only imageLayer will be masked
imageLayer.cornerRadius = 20.0
imageLayer.contentsGravity = .resizeAspectFill
view.layer.addSublayer(imageLayer)
self.view.addSubview(view)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment