Используя расширение UIView
, мне удалось добиться этого в одном из моих приложений:
extension UIView {
open func generateOuterShadow() {
let view = UIView()
view.translatesAutoresizingMaskIntoConstraints = false
view.layer.cornerRadius = layer.cornerRadius
view.layer.shadowRadius = layer.shadowRadius
view.layer.shadowOpacity = layer.shadowOpacity
view.layer.shadowColor = layer.shadowColor
view.layer.shadowOffset = CGSize.zero
view.clipsToBounds = false
view.backgroundColor = .white
superview?.insertSubview(view, belowSubview: self)
let constraints = [
NSLayoutConstraint(item: view, attribute: .left, relatedBy: .equal, toItem: self, attribute: .left, multiplier: 1.0, constant: 0.0),
NSLayoutConstraint(item: view, attribute: .right, relatedBy: .equal, toItem: self, attribute: .right, multiplier: 1.0, constant: 0.0),
NSLayoutConstraint(item: view, attribute: .top, relatedBy: .equal, toItem: self, attribute: .top, multiplier: 1.0, constant: 0.0),
NSLayoutConstraint(item: view, attribute: .bottom, relatedBy: .equal, toItem: self, attribute: .bottom, multiplier: 1.0, constant: 0.0),
]
superview?.addConstraints(constraints)
}
}
Использование:
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
shadowView.generateOuterShadow() // Add
}