title: Multiple shadows under UIView iOS Swift
body: <p>I am trying to make a UIButton with rounded corners that has 2 colored shadows. Why is the red (and at this point also the blue &quot;shadow&quot; layer covering the button? How to get the shadows below the button canvas). I thought it was helping to insert sublayers instead of just adding them.</p>
  <p><a href="https://i.stack.imgur.com/Z6PrC.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Z6PrC.png" alt="Shadows are above button" /></a></p>
  <p>I have made a playground illustrating the issue</p>
  <pre><code>import UIKit
  import PlaygroundSupport
  </code></pre>
  <p>This is the button I'm trying to implement</p>
  <pre><code>class PrimaryButton: UIButton {
      required init(text: String = &quot;Test 1&quot;, hasShadow: Bool = true) {
          super.init(frame: .zero)
          setTitle(text, for: .normal)
          backgroundColor = UIColor.blue
          layer.cornerRadius = 48 / 2
          layer.masksToBounds = false
          if hasShadow {
              insertShadow()
          }
      }
      
      fileprivate func insertShadow() {
          let layer2 = CALayer(layer: layer), layer3 = CALayer(layer: layer)
          layer2.applySketchShadow(color: UIColor.red, alpha: 0.5, x: 0, y: 15, blur: 35, spread: -10)
          layer3.applySketchShadow(color: UIColor.blue, alpha: 0.5, x: 0, y: 10, blur: 21, spread: -9)
          layer.insertSublayer(layer2, at: 0)
          layer.insertSublayer(layer3, at: 0)
      }
      
      required init?(coder aDecoder: NSCoder) {
          fatalError(&quot;init(coder:) has not been implemented&quot;)
      }
      
      override func layoutSubviews() {
          super.layoutSubviews()
          layer.sublayers?.forEach { (sublayer) in
              sublayer.shadowPath = UIBezierPath(rect: bounds).cgPath
          }
      }
  }
  </code></pre>
  <p>This is an extension that helps adding the shadow from Sketch specification:</p>
  <pre><code>extension CALayer {
      func applySketchShadow(
          color: UIColor = .black,
          alpha: Float = 0.5,
          x: CGFloat = 0,
          y: CGFloat = 2,
          blur: CGFloat = 4,
          spread: CGFloat = 0)
      {
          shadowColor = color.cgColor
          shadowOpacity = alpha
          shadowOffset = CGSize(width: x, height: y)
          shadowRadius = blur / 2.0
          if spread == 0 {
              shadowPath = nil
          } else {
              let dx = -spread
              let rect = bounds.insetBy(dx: dx, dy: dx)
              shadowPath = UIBezierPath(rect: rect).cgPath
          }
          masksToBounds = false
      }
  }
  
  
  class MyViewController : UIViewController {
      override func loadView() {
          let view = UIView()
          view.backgroundColor = .white
          
          let button = PrimaryButton()
          button.frame = CGRect(x: 150, y: 200, width: 200, height: 48)
          view.addSubview(button)
          self.view = view
      }
  }
  // Present the view controller in the Live View window
  PlaygroundPage.current.liveView = MyViewController()
  </code></pre>