Skip to content

Instantly share code, notes, and snippets.

@dymx101
Created April 2, 2020 08:26
Show Gist options
  • Save dymx101/c640da95e0f5400168cbd5674056aabc to your computer and use it in GitHub Desktop.
Save dymx101/c640da95e0f5400168cbd5674056aabc to your computer and use it in GitHub Desktop.
A container view helps drop shadow
class ShadowContainer: UIView {
init(child: UIView,
shadowRadius: CGFloat = 2,
shadowOffset: CGSize = CGSize.zero,
shadowOpacity: Float = 0.3) {
super.init(frame: CGRect.zero)
translatesAutoresizingMaskIntoConstraints = false
addSubview(child)
child.leadingAnchor.constraint(equalTo: leadingAnchor).isActive = true
child.trailingAnchor.constraint(equalTo: trailingAnchor).isActive = true
child.topAnchor.constraint(equalTo: topAnchor).isActive = true
child.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true
layer.shadowRadius = shadowRadius
layer.shadowOffset = shadowOffset
layer.shadowOpacity = shadowOpacity
layer.cornerRadius = child.layer.cornerRadius
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment