[ACCEPTED]-Shadow UIview and clipsToBounds-uikit
Accepted answer
the clipsToBounds
also clips your shadow. In order to 2 prevent this you can add _containerView.layer.masksToBounds = NO
which disables 1 clipping of sublayers (see more here).
If you have to use clipsToBounds = true
because you don't want 3 subviews to exceed your view's border, but 2 at the same time you need a shadow on your 1 view, I recommend adding an extra view behind your view and set the shadow properties on the extra view.
//run this in viewDidLoad() or your initialisation code
private func setupShadowContainer() {
let containerShadow = UIView()
parentView.addSubview(containerShadow)
containerShadow.dropShadow()
//using SnapKit here, you can use NSLayoutConstraint in a similar way to constraint the containerShadow behind your View
containerShadow.snp.makeConstraints { (make) in
make.edges.equalTo(yourView.snp.edges)
}
}
//dropShadow method
extension UIView {
func dropShadow() {
self.translatesAutoresizingMaskIntoConstraints = false
self.layer.shadowRadius = 3
self.layer.shadowColor = UIColor.black.cgColor
self.layer.shadowOffset = CGSize(width: 1.0, height: 1.0)
self.layer.shadowOpacity = 0.5
self.layer.masksToBounds = false
}
}
Here is the UIView extension.
extension UIView {
func addShadowAndRoundCorner(cornerRadius : CGFloat) {
self.layer.shadowOffset = .zero
self.layer.shadowOpacity = 0.5
self.layer.shadowRadius = 3
self.layer.shadowColor = UIColor.black.cgColor
self.layer.masksToBounds = false
self.layer.cornerRadius = cornerRadius
}
}
Call this function 1 after creation of view as follows:
let roundView = UIView()
roundView.frame = CGRect.init(x: 0, y: 0, width: 100, height: 100)
roundView.addShadowAndRoundCorner(cornerRadius: 100/2)
Source:
stackoverflow.com
More Related questions
Cookie Warning
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.