Created
April 27, 2020 17:08
-
-
Save alexnikol/10541c882268bdacdf16ee9fe72205b6 to your computer and use it in GitHub Desktop.
How to Create UIView with shadow, image, border and cornerRadius
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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