0

I have the below extension that allows me to round specific corners of a view:

extension CALayer {

    func roundCorners(corners: UIRectCorner, radius: CGFloat, viewBounds: CGRect) {

        let maskPath = UIBezierPath(roundedRect: viewBounds,
                                    byRoundingCorners: corners,
                                    cornerRadii: CGSize(width: radius, height: radius))

        let shape = CAShapeLayer()
        shape.path = maskPath.cgPath
        mask = shape
    }
}

Which is then used like:

innerView.layer.roundCorners(corners: [.bottomLeft, .bottomRight], radius: 20, viewBounds: innerView.bounds)

And works perfectly.

However if I then also want to add a shadow to that view the shadow isn't drawn, so for example:

override func viewDidAppear(_ animated: Bool) {
...
        innerView.layer.roundCorners(corners: [.bottomLeft, .bottomRight], radius: 20, viewBounds: innerView.bounds)
innerView.layer.shadowColor = UIColor.gray.cgColor
        innerView.layer.shadowOpacity = 0.7
        innerView.layer.shadowOffset = CGSize(width: 1.0, height: 1.0)
        innerView.layer.shadowRadius = 2
...
}

Results in the view just having the corners rounded but no shadow. How to do I create both the required shadow and be able to round specific corners (not just all 4 corners at once)

DevWithZachary
  • 3,545
  • 11
  • 49
  • 101
  • Hi, it's not straightforward. There is a long discussion about this here: https://stackoverflow.com/questions/4754392/uiview-with-rounded-corners-and-drop-shadow/50123586#50123586. If you look at my post at the end, that is working example for Swift 4, would could be adapted to any view. – rbaldwin May 03 '18 at 14:35
  • I dont believe your answer solves my issue as I need to be able to specifically round certain corners as the extension im using allows. – DevWithZachary May 03 '18 at 14:39
  • You could try creating a custom containerView and a customView, and add the customView as a subView of the containerView ? – rbaldwin May 03 '18 at 14:42
  • I could, and I could all create a second view with the required offset behind the main view to act as the shadow but they all seem like rather dirty methods (which of course I will have to use if needed) – DevWithZachary May 03 '18 at 14:44
  • As you will see from the discussion, there does not appear to be easy way to do this at present. – rbaldwin May 03 '18 at 14:46

0 Answers0