1

Is there a way to apply rounded corners to certain corners only, and at the same time apply a shadow layer?

I tried doing that using the following extension, but that didn't work for me:

extension UIView {
    func roundCorners(_ corners: UIRectCorner, radius: CGFloat, shadow: Bool = false) {
        let path = UIBezierPath(roundedRect: self.bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius))
        let mask = CAShapeLayer()
        mask.path = path.cgPath
        self.layer.mask = mask
        if shadow {
            self.layer.shadowColor = UIColor.black.cgColor
            self.layer.shadowOffset = CGSize(width: 0, height: 2)
            self.layer.shadowOpacity = 0.3
            self.layer.shadowRadius = 3.0
        }
    }
}
  • [Example](https://stackoverflow.com/questions/10167266/how-to-set-cornerradius-for-only-top-left-and-top-right-corner-of-a-uiview), [example](https://www.hackingwithswift.com/example-code/calayer/how-to-round-only-specific-corners-using-maskedcorners), [example](https://www.appcoda.com/rounded-corners-uiview/) and [shadowing rounded corners](https://stackoverflow.com/questions/52431760/how-to-round-a-shadow-in-ios/52432975#52432975) – MadProgrammer Feb 17 '19 at 00:03
  • 1
    @MadProgrammer Yes I know, but they don't apply a shadow layer over their corners. The rounded corner part is working, so is the shadow part, but together, they are not. – FrankHeijden Feb 17 '19 at 00:06

1 Answers1

0

So, I tossed in some code into playground and had a quick playground, based on the example from How to round only specific corners using maskedCorners

let view = UIView(frame: CGRect(x: 100, y: 100, width: 500, height: 500))
view.backgroundColor = UIColor.white
view.layer.cornerRadius = 15
view.layer.maskedCorners = [.layerMinXMinYCorner, .layerMaxXMaxYCorner]

view.layer.shadowColor = UIColor.black.cgColor
view.layer.shadowOffset = CGSize(width: 0, height: 0)
view.layer.shadowOpacity = 1.0
view.layer.shadowRadius = 4
//view.layer.shadowPath = UIBezierPath(rect: view.bounds).cgPath
view.layer.shouldRasterize = false


let outter = UIView(frame: CGRect(x: 0, y: 0, width: 700, height: 700))
outter.backgroundColor = UIColor.red
outter.addSubview(view)


outter

I did some testing:

With no corner radius

No corner Radius

With masked corner radius

With masked corner

With reduced shawdowRadius

Reduced shadowRadius

And just to be sure, increased corner radius

Increased corner radius

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • Ok, so I have a demonstrable example showing the concept requested - so, why the downvote? Since there's no other answer, I assume there's no better ideas? – MadProgrammer Mar 11 '20 at 10:19