Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add shadow to a transparent UIView?

How can I reproduce this shadowing? My problem, that if I use high alpha color or clearColor, there is no drop shadow, if I use low alpha color, I can't see the drop shadow under the plate. As the level of the volume is changing (colorful part) the shadow is moving also, so it is not pure Photoshop.

enter image description here

like image 751
János Avatar asked Jun 02 '12 09:06

János


People also ask

How do I add a shadow to a view in Java?

To add a shadow to our new view, we need to add the following lines of code to our newView () method just below the view.backgroundColor = .blue: view. layer. shadowOffset = CGSize( width: 10, height: 10) view. layer. shadowRadius = 5 view. layer. shadowOpacity = 0.3

How do I add a shadow to a containerview?

The shadow is applied to the parent view ’s layer, while the rounded corners are applied to the containerView. Then, just add all content to the containerView and be on your way.

Why can’t we use the same layer for a shadow?

A shadow will be drawn outside of the layer, thus it is clipped, too. So, we cannot use the same layer for both of these effects. I’ll share what I found to be the path of least resistance.

What is a shadowpath in UIKit?

This means that UIKit has needed to calculate the shadows path on the fly. Creating a shadow in this way is a very expensive task, and when depending on the device and the number of views that need shadows, doing this can decrease performance dramatically. To fix this we can use a shadowPath.


2 Answers

try something using thoses properies of your view. it might end up with something

    view.layer.shadowColor = [UIColor colorWithWhite:.5 alpha:1].CGColor;
    view.layer.shadowRadius = 4.0f;
    view.layer.shadowPath = CGPathCreateWithRect(CGRectMake(0, 0, 50, 50), NULL);
    view.layer.shadowOpacity = 1.0f;
    view.layer.shadowOffset = CGSizeMake(1, 1);

it's leaking by the way should CGPathRelease(rect) at the end sorry.

like image 131
Nicolas Manzini Avatar answered Sep 19 '22 05:09

Nicolas Manzini


After searching and not finding an answer to this question I wrote the following extension in Swift 5:

extension UIView {
    func makeClearViewWithShadow(
        cornderRadius: CGFloat,
        shadowColor: CGColor,
        shadowOpacity: Float,
        shadowRadius: CGFloat) {

        self.frame = self.frame.insetBy(dx: -shadowRadius * 2,
                                        dy: -shadowRadius * 2)
        self.backgroundColor = .clear
        let shadowView = UIView(frame: CGRect(
            x: shadowRadius * 2,
            y: shadowRadius * 2,
            width: self.frame.width - shadowRadius * 4,
            height: self.frame.height - shadowRadius * 4))
        shadowView.backgroundColor = .black
        shadowView.layer.cornerRadius = cornderRadius
        shadowView.layer.borderWidth = 1.0
        shadowView.layer.borderColor = UIColor.clear.cgColor

        shadowView.layer.shadowColor = shadowColor
        shadowView.layer.shadowOpacity = shadowOpacity
        shadowView.layer.shadowRadius = shadowRadius
        shadowView.layer.masksToBounds = false
        self.addSubview(shadowView)

        let p:CGMutablePath = CGMutablePath()
        p.addRect(self.bounds)
        p.addPath(UIBezierPath(roundedRect: shadowView.frame, cornerRadius: shadowView.layer.cornerRadius).cgPath)

        let s = CAShapeLayer()
        s.path = p
        s.fillRule = CAShapeLayerFillRule.evenOdd

       self.layer.mask = s
    }
}

Usage Example:

myView.makeClearViewWithShadow(cornderRadius: 20, shadowColor: UIColor.black.cgColor, shadowOpacity: 0.5, shadowRadius: 30)
like image 41
Ross Krasner Avatar answered Sep 19 '22 05:09

Ross Krasner